]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/format/flv.cpp
3ebcc8ec0f330f2764506357b5d96cf3e09b6045
[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 namespace caspar {\r
12         \r
13 double to_double(std::vector<char> bytes, bool readInReverse)\r
14 {\r
15     if(bytes.size() != 8)\r
16                 BOOST_THROW_EXCEPTION(caspar_exception());\r
17 \r
18     if (readInReverse)\r
19                 std::reverse(bytes.begin(), bytes.end());\r
20         \r
21         static_assert(sizeof(double) == 8, "");\r
22 \r
23         double* tmp = (double*)bytes.data();\r
24         \r
25         double val = *tmp;\r
26     return val;\r
27 }\r
28 \r
29 double next_double(std::fstream& fileStream, int offset, int length)\r
30 {\r
31     fileStream.seekg(offset, std::ios::cur);\r
32         std::vector<char> bytes(length);\r
33     fileStream.read(bytes.data(), length);\r
34     return to_double(bytes, true);\r
35 }\r
36 \r
37 flv_meta_info read_flv_meta_info(const std::wstring& filename)\r
38 {\r
39         if(!boost::filesystem::exists(filename))\r
40                 BOOST_THROW_EXCEPTION(caspar_exception());\r
41 \r
42         flv_meta_info meta_info;\r
43 \r
44     std::fstream fileStream = std::fstream(narrow(filename), std::fstream::in);\r
45         fileStream.seekg(27, std::ios::beg);\r
46         \r
47     std::array<char, 10> bytes;\r
48     fileStream.read(bytes.data(), bytes.size());\r
49 \r
50         auto on_meta_data = std::string(bytes.begin(), bytes.end());   \r
51         if (on_meta_data == "onMetaData")\r
52     {\r
53         //// 16 bytes past "onMetaData" is the data for "duration" \r
54         meta_info.duration = next_double(fileStream, 16, 8);\r
55         //// 8 bytes past "duration" is the data for "width"\r
56         meta_info.width = next_double(fileStream, 8, 8);\r
57         //// 9 bytes past "width" is the data for "height"\r
58         meta_info.height = next_double(fileStream, 9, 8);\r
59         //// 16 bytes past "height" is the data for "videoDataRate"\r
60         meta_info.video_data_rate = next_double(fileStream, 16, 8);\r
61         //// 16 bytes past "videoDataRate" is the data for "audioDataRate"\r
62         meta_info.audio_data_rate = next_double(fileStream, 16, 8);\r
63         //// 12 bytes past "audioDataRate" is the data for "frameRate"\r
64         meta_info.frame_rate = next_double(fileStream, 12, 8);\r
65     }\r
66     \r
67     return meta_info;\r
68 }\r
69 \r
70 }