mirror of
https://github.com/k4yt3x/video2x.git
synced 2026-02-27 18:54:48 +08:00
perf(libvideo2x): removed unnecessary read of nb_frames
Signed-off-by: k4yt3x <i@k4yt3x.com>
This commit is contained in:
@@ -47,31 +47,15 @@ int process_frames(
|
|||||||
std::vector<AVFrame *> flushed_frames;
|
std::vector<AVFrame *> flushed_frames;
|
||||||
char errbuf[AV_ERROR_MAX_STRING_SIZE];
|
char errbuf[AV_ERROR_MAX_STRING_SIZE];
|
||||||
|
|
||||||
// Get the total number of frames in the video
|
// Get the total number of frames in the video with OpenCV
|
||||||
AVStream *video_stream = ifmt_ctx->streams[vstream_idx];
|
spdlog::debug("Unable to estimate total number of frames; reading with OpenCV");
|
||||||
proc_ctx->total_frames = video_stream->nb_frames;
|
cv::VideoCapture cap(ifmt_ctx->url);
|
||||||
|
if (!cap.isOpened()) {
|
||||||
// If nb_frames is not set, estimate total frames using duration and frame rate
|
spdlog::error("Failed to open video file with OpenCV");
|
||||||
if (proc_ctx->total_frames == 0) {
|
return -1;
|
||||||
spdlog::debug("`nb_frames` is not set; estimating total frames with duration*framerate");
|
|
||||||
int64_t duration = video_stream->duration;
|
|
||||||
AVRational frame_rate = video_stream->avg_frame_rate;
|
|
||||||
if (duration != AV_NOPTS_VALUE && frame_rate.num != 0 && frame_rate.den != 0) {
|
|
||||||
proc_ctx->total_frames = duration * frame_rate.num / frame_rate.den;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If total_frames is still 0, read the total number of frames with OpenCV
|
|
||||||
if (proc_ctx->total_frames == 0) {
|
|
||||||
spdlog::debug("Unable to estimate total number of frames; reading with OpenCV");
|
|
||||||
cv::VideoCapture cap(ifmt_ctx->url);
|
|
||||||
if (!cap.isOpened()) {
|
|
||||||
spdlog::error("Failed to open video file with OpenCV");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
proc_ctx->total_frames = cap.get(cv::CAP_PROP_FRAME_COUNT);
|
|
||||||
cap.release();
|
|
||||||
}
|
}
|
||||||
|
proc_ctx->total_frames = cap.get(cv::CAP_PROP_FRAME_COUNT);
|
||||||
|
cap.release();
|
||||||
|
|
||||||
// Check if the total number of frames is still 0
|
// Check if the total number of frames is still 0
|
||||||
if (proc_ctx->total_frames == 0) {
|
if (proc_ctx->total_frames == 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user