]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/util/util.cpp
Updated CHANGES.txt and fixed minor problems
[casparcg] / modules / ffmpeg / producer / util / util.cpp
index b34f0e29ad922103bda94f8f50b5044b56786847..92fd8c7f0e29ed4e5aac086782acf16d9e5dea14 100644 (file)
@@ -336,7 +336,15 @@ double read_fps(AVFormatContext& context, double fail_value)
        {\r
                const auto video_context = context.streams[video_index]->codec;\r
                const auto video_stream  = context.streams[video_index];\r
-                                               \r
+                               \r
+               auto frame_rate_time_base = video_stream->avg_frame_rate;\r
+               std::swap(frame_rate_time_base.num, frame_rate_time_base.den);\r
+\r
+               if(is_sane_fps(frame_rate_time_base))\r
+               {\r
+                       return static_cast<double>(frame_rate_time_base.den) / static_cast<double>(frame_rate_time_base.num);\r
+               }\r
+\r
                AVRational time_base = video_context->time_base;\r
 \r
                if(boost::filesystem2::path(context.filename).extension() == ".flv")\r
@@ -465,6 +473,34 @@ bool is_valid_file(const std::wstring filename)
        return is_valid_file(filename, invalid_exts);\r
 }\r
 \r
+bool try_get_duration(const std::wstring filename, std::int64_t& duration, boost::rational<std::int64_t>& time_base)\r
+{              \r
+       AVFormatContext* weak_context = nullptr;\r
+       if(avformat_open_input(&weak_context, narrow(filename).c_str(), nullptr, nullptr) < 0)\r
+               return false;\r
+\r
+       std::shared_ptr<AVFormatContext> context(weak_context, av_close_input_file);\r
+       \r
+       context->probesize = context->probesize / 10;\r
+       context->max_analyze_duration = context->probesize / 10;\r
+\r
+       if(avformat_find_stream_info(context.get(), nullptr) < 0)\r
+               return false;\r
+\r
+       const auto fps = read_fps(*context, 1.0);\r
+               \r
+       const auto rational_fps = boost::rational<std::int64_t>(static_cast<int>(fps * AV_TIME_BASE), AV_TIME_BASE);\r
+       \r
+       duration = boost::rational_cast<std::int64_t>(context->duration * rational_fps / AV_TIME_BASE);\r
+\r
+       if (rational_fps == 0)\r
+               return false;\r
+\r
+       time_base = 1/rational_fps;\r
+\r
+       return true;\r
+}\r
+\r
 std::wstring probe_stem(const std::wstring stem, const std::vector<std::wstring>& invalid_exts)\r
 {\r
        auto stem2 = boost::filesystem2::wpath(stem);\r