使用 FFmpeg 时将视频分割成多个少于 10 秒的部分时出现问题

使用 FFmpeg 时将视频分割成多个少于 10 秒的部分时出现问题

我正在尝试将视频分割成多个部分,但是,当我将片段大小设置为小于 10 秒时,它会生成 10 秒的片段。如果片段大小大于 10 秒,它就会起作用。这是我正在使用的命令,

ffmpeg -i HVC656729.mp4 -acodec copy -f segment -segment_time 5 -vcodec copy -reset_timestamps 1 -map 0 -an fff%d.mp4

我也尝试了另一种选择:

ffmpeg -i 'HVC656729.mp4'  -strict -2 -vcodec copy -ss 0 -t 5 '0.mp4'
ffmpeg -i ’HVC656729.mp4'  -strict -2 -vcodec copy -ss 5 -t 5 '1.mp4'
ffmpeg -i 'HVC656729.mp4'  -strict -2 -vcodec copy -ss 10 -t 5 '2.mp4'
ffmpeg -i 'HVC656729.mp4'  -strict -2 -vcodec copy -ss 15 -t 5 '3.mp4'
ffmpeg -i 'HVC656729.mp4'  -strict -2 -vcodec copy -ss 20 -t 5 '4.mp4'
ffmpeg -i 'HVC656729.mp4'  -strict -2 -vcodec copy -ss 25 -t 5 '5.mp4'
ffmpeg -i 'HVC656729.mp4'  -strict -2 -vcodec copy -ss 30 -t 5 '6.mp4'
ffmpeg -i 'HVC656729.mp4'  -strict -2 -vcodec copy -ss 35 -t 5 '7.mp4'
ffmpeg -i 'HVC656729.mp4'  -strict -2 -vcodec copy -ss 40 -t 5 '8.mp4'
ffmpeg -i 'HVC656729.mp4'  -strict -2 -vcodec copy -ss 45 -t 5 '9.mp4'
ffmpeg -i 'HVC656729.mp4'  -strict -2 -vcodec copy -ss 50 -t 5 '10.mp4’

依此类推,直到视频长度,但是,在这种情况下,奇数个视频只包含音频而不包含视频,而偶数个视频包含视频。当我将持续时间设置为 10 秒时,这个东西也开始工作了。我不确定里面发生了什么,因为 ffmpeg 文档还说默认的segment_size 是 2 秒ffmpeg 链接,我不知道为什么会发生这种情况。

视频如果你需要参考可以在这里看到

第一个命令的输出如下:

ffmpeg -i /lustre/bharat/devo/HVC656729.mp4 -acodec copy -f segment -segment_time 5 -vcodec copy -reset_timestamps 1 -map 0 -an fff%d.mp4              
ffmpeg version 2.0.2 Copyright (c) 2000-2013 the FFmpeg developers
  built on Oct 23 2013 08:49:53 with gcc 4.6.1 (GCC)
  configuration: --prefix=/usr/local/ffmpeg/2.0.2 --libdir=/usr/local/ffmpeg/lib
  libavutil      52. 38.100 / 52. 38.100
  libavcodec     55. 18.102 / 55. 18.102
  libavformat    55. 12.100 / 55. 12.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 79.101 /  3. 79.101
  libswscale      2.  3.100 /  2.  3.100
  libswresample   0. 17.102 /  0. 17.102
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/lustre/bharat/devo/HVC656729.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 1970-01-01 00:00:00
    encoder         : Lavf52.68.0
  Duration: 00:02:57.39, start: 0.000000, bitrate: 637 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 320x240, 504 kb/s, 23.96 fps, 23.98 tbr, 24k tbn, 47.95 tbc
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
Output #0, segment, to 'fff%d.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf55.12.100
    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p, 320x240, q=2-31, 504 kb/s, 23.96 fps, 90k tbn, 23.98 tbc
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
frame= 4250 fps=0.0 q=-1.0 Lsize=N/A time=00:02:57.34 bitrate=N/A    
video:10934kB audio:0kB subtitle:0 global headers:0kB muxing overhead -100.000196%

奇数版本的输出(第二条命令):

ffmpeg -i '/lustre/bharat/devo/HVC656729.mp4' -strict -2 -vcodec copy -ss 5 -t 5 '1.mp4'
ffmpeg version 2.0.2 Copyright (c) 2000-2013 the FFmpeg developers
  built on Oct 23 2013 08:49:53 with gcc 4.6.1 (GCC)
  configuration: --prefix=/usr/local/ffmpeg/2.0.2 --libdir=/usr/local/ffmpeg/lib
  libavutil      52. 38.100 / 52. 38.100
  libavcodec     55. 18.102 / 55. 18.102
  libavformat    55. 12.100 / 55. 12.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 79.101 /  3. 79.101
  libswscale      2.  3.100 /  2.  3.100
  libswresample   0. 17.102 /  0. 17.102
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/lustre/bharat/devo/HVC656729.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 1970-01-01 00:00:00
    encoder         : Lavf52.68.0
  Duration: 00:02:57.39, start: 0.000000, bitrate: 637 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 320x240, 504 kb/s, 23.96 fps, 23.98 tbr, 24k tbn, 47.95 tbc
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
Output #0, mp4, to '1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf55.12.100
    Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 320x240, q=2-31, 504 kb/s, 23.96 fps, 24k tbn, 24k tbc
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (aac -> aac)
Press [q] to stop, [?] for help
frame=    0 fps=0.0 q=-1.0 Lsize=      80kB time=00:00:05.01 bitrate= 130.8kbits/s    
video:0kB audio:78kB subtitle:0 global headers:0kB muxing overhead 2.015301%

偶数版本的输出:

ffmpeg -i '/lustre/bharat/devo/HVC656729.mp4' -strict -2 -vcodec copy -ss 0 -t 5 '0.mp4'
ffmpeg version 2.0.2 Copyright (c) 2000-2013 the FFmpeg developers
  built on Oct 23 2013 08:49:53 with gcc 4.6.1 (GCC)
  configuration: --prefix=/usr/local/ffmpeg/2.0.2 --libdir=/usr/local/ffmpeg/lib
  libavutil      52. 38.100 / 52. 38.100
  libavcodec     55. 18.102 / 55. 18.102
  libavformat    55. 12.100 / 55. 12.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 79.101 /  3. 79.101
  libswscale      2.  3.100 /  2.  3.100
  libswresample   0. 17.102 /  0. 17.102
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/lustre/bharat/devo/HVC656729.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 1970-01-01 00:00:00
    encoder         : Lavf52.68.0
  Duration: 00:02:57.39, start: 0.000000, bitrate: 637 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 320x240, 504 kb/s, 23.96 fps, 23.98 tbr, 24k tbn, 47.95 tbc
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
Output #0, mp4, to '0.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf55.12.100
    Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 320x240, q=2-31, 504 kb/s, 23.96 fps, 24k tbn, 24k tbc
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (aac -> aac)
Press [q] to stop, [?] for help
frame=  119 fps=0.0 q=-1.0 Lsize=     386kB time=00:00:05.01 bitrate= 630.4kbits/s    
video:302kB audio:80kB subtitle:0 global headers:0kB muxing overhead 1.218538%

答案1

我非常确定您的视频的 GOP 长度至少为 10 秒。换句话说,每 10 秒才会有一个 IDR 帧(解码器可以独立于其他帧启动的 I 帧)。

这意味着 ffmpeg 无法将视频分成更短的片段,因为片段必须以 IDR 帧开始。

为了解决这个问题,你必须重新编码视频流,设置固定的 GOP 长度,例如每秒 24 fps 的视频:

ffmpeg -i input -c:v libx264 \
-r 24 -x264opts keyint=24:min-keyint=24 -crf 19 \
out.mp4

当然你也可以直接进行拆分,而不是输出到out.mp4

设置CRF来控制质量——检查H.264 编码指南了解更多信息。

相关内容