ffmpeg concat videos with different timebase

六眼飞鱼酱① 提交于 2019-12-10 12:04:44

问题


I use

ffmpeg.exe -f concat -i file_path_list_txt -c copy out_out.mp4

to concat

for file in 1265_*; do ffmpeg -i $file -crf 30 -b:a 23k -b:v 96k -threads 3 -y 'out_'$file; done

compressed video.

When I play the generated video, the player shows the video length is much longer than the sum of compressed video pieces. And at the linkage between slices,the frame can play a very long time,time on the player is going on,but the frame is still.

I use ffprobe to show the original video pieces and compressed video pieces. And found the original videos with same tbr,tbn,tbc, while the compressed not.

Original videos:

Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, progressive), 1600x1144, 134 kb/s, 8.17 fps, 600 tbr, 600 tbn, 1200 tbc (default)
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, progressive), 1600x1144, 138 kb/s, 9.73 fps, 600 tbr, 600 tbn, 1200 tbc (default)

Compressed videos:

Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1600x1144, 174 kb/s, 8.17 fps, 8.17 tbr, 245050 tbn, 16.33 tbc (default)
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1600x1144, 127 kb/s, 9.73 fps, 9.73 tbr, 1750800 tbn, 19.45 tbc (default)

And when I concat, in the console,ffmpeg show below info:

[mp4 @ 000001fe0193b700] Application provided duration: 2585263321 / timestamp: 6303355764 is out of range for mov/mp4 format
[mp4 @ 000001fe0193b700] pts has no value
[mp4 @ 000001fe0193b700] Application provided duration: 2585443321 / timestamp: 6303535765 is out of range for mov/mp4 format
[mp4 @ 000001fe0193b700] pts has no value

In my situation the video pieces are generated time by time, and I don't know when it stops, so I cannot do concat before compress. Each time a video piece generated I comress it and append it.


回答1:


When compressing to MOV/MP4, set a common timescale tbn using -video_track_timescale N. For your input, N=600 looks fine.




回答2:


With the help of Gyan's answer,I make it works,both the compress and append process I need to set tbn.

Details as below:

Each time a new video slice generated, get tbn with get_video_attributes, then compress it with,

ffmpeg -i $video_path_i -crf 30 -b:a 23k -b:v 96k -video_track_timescale $tbn -threads 3 -y 'out_'$file

Then append the compressed slice to previous video with, ffmpeg.exe -f concat -i file_path_list_txt -video_track_timescale $tbn -c copy out_out.mp4

Update get_video_attribute function according to llogan 's comment

function get_video_attribute(&$video_attribute,$video_path,$ffprobe="ffprobe"){

    $command = "$ffprobe -v error -show_entries stream=codec_type,codec_name,width,height,bit_rage,avg_frame_rate,r_frame_rate,".
        "channels,channel_layout,sample_rate,bit_rate,time_base,codec_time_base,duration:format=duration,size,bit_rate -of json $video_path";
    $video_attribute = [];
    exec($command,$output,$return_var);
    if ($return_var) {
        return false;
    }
    if(false !== ($attribute=json_decode(join($output),true))){
        // TODO video file may have more streams or less streams than one video and one audio stream
        foreach($attribute["streams"] as $stream){
            if(!isset(${$stream["codec_type"]})){
                ${$stream["codec_type"]} = $stream;
            }
        }
        $video_attribute = array(
            'width' => $video["width"],
            'height' => $video["height"],
            'duration' => intval($attribute["format"]["duration"]),
            'v_codec' => $video["codec_name"],
            'a_codec' => $audio["codec_name"],
            "tbn" => $video["time_base"] ? explode("/",$video["time_base"])[1] : null,
            'bps' => $attribute["format"]["bit_rate"],
            'v_bps' => $video["bit_rate"],
            'a_bps' => $audio["bit_rate"]
        );
        return true;
    }else{
        return false;
    }
}


来源:https://stackoverflow.com/questions/57018278/ffmpeg-concat-videos-with-different-timebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!