ffmpeg concat videos with different timebase

无人久伴 提交于 2019-12-01 14:45:49

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

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