代码之家  ›  专栏  ›  技术社区  ›  LF-DevJourney

ffmpeg concat压缩视频导致错误的时间跨度

  •  0
  • LF-DevJourney  · 技术社区  · 7 年前

    我用

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

    干杯

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

    压缩视频。

    当我播放生成的视频时,播放器显示视频长度远大于压缩视频片段的总和。在切片之间的连接处,帧可以播放很长时间,时间在播放器上进行,但帧仍然是。

    我用 ffprobe 显示原始视频片段和压缩视频片段。找到了同样的原始视频 tbr,tbn,tbc ,而压缩的。

    原创视频:

    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)
    
    

    压缩视频:

    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)
    
    

    当我在控制台中搜索时,ffmpeg会显示以下信息:

    [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
    

    在我的情况下,视频片段是一次又一次地生成的,我不知道它何时停止,所以我不能在压缩之前进行concat。每次生成视频片段时,我都会将其压缩并附加。

    0 回复  |  直到 7 年前
        1
  •  1
  •   Gyan    7 年前

    tbn 使用 -video_track_timescale N .对于您的输入,N=600看起来很好。

        2
  •  1
  •   LF-DevJourney    7 年前

    借助于 Gyan's answer ,我让它工作,压缩和附加过程我需要设置 tbn .

    详情如下:

    每次生成新的视频片段时,获取 tbn 具有 get_video_attributes ,然后用,

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

    然后用, ffmpeg.exe -f concat -i file_path_list_txt -video_track_timescale $tbn -c copy out_out.mp4

    使现代化 get_video_attribute 根据 llogan 奥巴马的评论

    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;
        }
    }