]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/format/flv.cpp
2.0. ffmpeg_producer: Added fix for reading FLV meta-data. FFMPEG reads it incorrectly.
[casparcg] / modules / ffmpeg / producer / format / flv.cpp
1 #include "../../stdafx.h"\r
2 \r
3 #include "flv.h"\r
4 \r
5 #include <common/exception/exceptions.h>\r
6 \r
7 #include <boost/filesystem.hpp>\r
8 \r
9 #include <iostream>\r
10 \r
11 #include <unordered_map>\r
12 \r
13 namespace caspar {\r
14         \r
15 double to_double(std::vector<char> bytes, bool readInReverse)\r
16 {\r
17     if(bytes.size() != 8)\r
18                 BOOST_THROW_EXCEPTION(caspar_exception());\r
19 \r
20     if (readInReverse)\r
21                 std::reverse(bytes.begin(), bytes.end());\r
22         \r
23         static_assert(sizeof(double) == 8, "");\r
24 \r
25         double* tmp = (double*)bytes.data();\r
26         \r
27         double val = *tmp;\r
28     return val;\r
29 }\r
30 \r
31 double next_double(std::fstream& fileStream)\r
32 {\r
33         std::vector<char> bytes(8);\r
34     fileStream.read(bytes.data(), bytes.size());\r
35     return to_double(bytes, true);\r
36\r
37 \r
38 bool next_bool(std::fstream& fileStream)\r
39 {\r
40         std::vector<char> bytes(1);\r
41     fileStream.read(bytes.data(), bytes.size());\r
42     return bytes[0] != 0;\r
43 }\r
44 \r
45 std::map<std::string, std::string> read_flv_meta_info(const std::string& filename)\r
46 {\r
47         std::map<std::string, std::string>  values;\r
48 \r
49         if(boost::filesystem2::path(filename).extension() != ".flv")\r
50                 return values;\r
51         \r
52         try\r
53         {\r
54                 if(!boost::filesystem2::exists(filename))\r
55                         BOOST_THROW_EXCEPTION(caspar_exception());\r
56         \r
57                 std::fstream fileStream = std::fstream(filename, std::fstream::in);\r
58                 fileStream.seekg(27, std::ios::beg);\r
59         \r
60                 std::vector<char> bytes(10);\r
61                 fileStream.read(bytes.data(), bytes.size());\r
62                 \r
63                 if (std::string(bytes.begin(), bytes.end()) == "onMetaData")\r
64                 {\r
65                         fileStream.seekg(6, std::ios::cur);\r
66 \r
67                         for(int n = 0; n < 9; ++n)\r
68                         {\r
69                                 char name_size = 0;\r
70                                 fileStream.read(&name_size, 1);\r
71 \r
72                                 std::vector<char> name(name_size);\r
73                                 fileStream.read(name.data(), name.size());\r
74                                 auto name_str = std::string(name.begin(), name.end());\r
75 \r
76                                 char data_type = 0;\r
77                                 fileStream.read(&data_type, 1);\r
78 \r
79                                 switch(data_type)\r
80                                 {\r
81                                 case 0:\r
82                                         values[name_str] = boost::lexical_cast<std::string>(next_double(fileStream));\r
83                                         break;\r
84                                 case 1:\r
85                                         values[name_str] = boost::lexical_cast<std::string>(next_bool(fileStream));\r
86                                         break;\r
87                                 }\r
88                                 fileStream.seekg(1, std::ios::cur);\r
89                         }\r
90                 }\r
91         }\r
92         catch(...)\r
93         {\r
94                 CASPAR_LOG_CURRENT_EXCEPTION();\r
95         }\r
96 \r
97     return values;\r
98 }\r
99 \r
100 }