如何修复 ffmpeg 不准确的时间戳导致缩略图生成失败

如何修复 ffmpeg 不准确的时间戳导致缩略图生成失败

所以这很有趣,我今天收到了一个上传的视频,时间戳超过一小时,但当我检查视频时,它只有 8 分钟长。

"C:/server/ffmpeg/bin/ffmpeg.exe" -i Z:/server/websites/ps/public_www/media/com_hwdmediashare/files/bd/47/a0/4a66f518323b18db8ff0ec3652c01914.mov 2>&1

ffmpeg version N-68639-g6706a29 Copyright (c) 2000-2014 the FFmpeg developers
  built on Dec 20 2014 22:12:53 with gcc 4.9.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-decklink --enable-zlib
  libavutil      54. 15.100 / 54. 15.100
  libavcodec     56. 16.100 / 56. 16.100
  libavformat    56. 16.101 / 56. 16.101
  libavdevice    56.  3.100 / 56.  3.100
  libavfilter     5.  4.100 /  5.  4.100
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
[wmv3 @ 0000000004479360] Extra data: 8 bits left, value: 0
[asf @ 0000000004490c40] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for  Input Stream #0.1 : stereo
Input #0, asf, from 'Z:/server/websites/ps/public_www/media/com_hwdmediashare/files/bd/47/a0/4a66f518323b18db8ff0ec3652c01914.mov':
  Metadata:
    WMFSDKNeeded    : 0.0.0.0000
    DeviceConformanceTemplate: L1
    WMFSDKVersion   : 12.0.7601.17514
    IsVBR           : 0
  Duration: 01:17:42.07, start: 0.000000, bitrate: 1096 kb/s
    Stream #0:0(eng): Video: wmv3 (Main) (WMV3 / 0x33564D57), yuv420p, 640x480, 1000 kb/s, 25 fps, 25 tbr, 1k tbn, 1k tbc
    Stream #0:1(eng): Audio: wmav2 (a[1][0][0] / 0x0161), 44100 Hz, 2 channels, fltp, 96 kb/s
At least one output file must be specified

这是缩略图生成的输入和输出。

"C:/server/ffmpeg/bin/ffmpeg.exe" -y -itsoffset -2331 -i Z:/server/websites/ps/public_www/media/com_hwdmediashare/files/bd/47/a0/4a66f518323b18db8ff0ec3652c01914.mov -vcodec mjpeg -vframes 1 -an -f rawvideo -s 134x100  Z:/server/websites/ps/public_www/media/com_hwdmediashare/files/bd/47/a0/32274bd09e981494f07357325dacdd7b.jpg 2>&1

ffmpeg version N-68639-g6706a29 Copyright (c) 2000-2014 the FFmpeg developers
  built on Dec 20 2014 22:12:53 with gcc 4.9.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-decklink --enable-zlib
  libavutil      54. 15.100 / 54. 15.100
  libavcodec     56. 16.100 / 56. 16.100
  libavformat    56. 16.101 / 56. 16.101
  libavdevice    56.  3.100 / 56.  3.100
  libavfilter     5.  4.100 /  5.  4.100
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
[wmv3 @ 000000000443c040] Extra data: 8 bits left, value: 0
[asf @ 0000000004439be0] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for  Input Stream #0.1 : stereo
Input #0, asf, from 'Z:/server/websites/ps/public_www/media/com_hwdmediashare/files/bd/47/a0/4a66f518323b18db8ff0ec3652c01914.mov':
  Metadata:
    WMFSDKNeeded    : 0.0.0.0000
    DeviceConformanceTemplate: L1
    WMFSDKVersion   : 12.0.7601.17514
    IsVBR           : 0
  Duration: 01:17:42.07, start: 0.000000, bitrate: 1096 kb/s
    Stream #0:0(eng): Video: wmv3 (Main) (WMV3 / 0x33564D57), yuv420p, 640x480, 1000 kb/s, 25 fps, 25 tbr, 1k tbn, 1k tbc
    Stream #0:1(eng): Audio: wmav2 (a[1][0][0] / 0x0161), 44100 Hz, 2 channels, fltp, 96 kb/s
[swscaler @ 00000000044300a0] deprecated pixel format used, make sure you did set range correctly
[wmv3 @ 00000000044c3f60] Extra data: 8 bits left, value: 0
Output #0, rawvideo, to 'Z:/server/websites/ps/public_www/media/com_hwdmediashare/files/bd/47/a0/32274bd09e981494f07357325dacdd7b.jpg':
  Metadata:
    WMFSDKNeeded    : 0.0.0.0000
    DeviceConformanceTemplate: L1
    WMFSDKVersion   : 12.0.7601.17514
    IsVBR           : 0
    encoder         : Lavf56.16.101
    Stream #0:0(eng): Video: mjpeg, yuvj420p(pc), 134x100, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc56.16.100 mjpeg
Stream mapping:
  Stream #0:0 -> #0:0 (wmv3 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=230    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=482    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=725    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=968    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=1216    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=1466    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=1714    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=1948    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=2206    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=2451    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=2694    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=2933    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=3168    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=3412    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=3657    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=3911    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=4158    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=4389    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=4625    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=4884    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=5141    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=5394    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=5645    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=5898    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=6170    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=6434    
frame=    0 fps=0.0 q=0.0

我知道如何修复时间戳不准确的视频,以免破坏我生成的缩略图吗?

如何使用 PHP 获取持续时间

preg_match('/Duration: (.*?),/', implode("\n",$output), $matches);
                    $duration_string = $matches[1];

                    list($hr,$m,$s) = explode(':', $duration_string);
                    $duration = ( (int)$hr*3600 ) + ( (int)$m*60 ) + (int)$s;
                    $duration = (int) $duration;

答案1

您通常可以通过创建视频的新副本(无需重新编码)来解决此问题:

ffmpeg -i input -c:v copy -c:a copy output

然后尝试从新视频生成缩略图。

答案2

这可能是由于视频中的 start_time 偏移造成的。您可以使用 ffprobe 来询问流的开始时间:

ffprobe -i <yourvideoname>.mp4 -select_streams v:0 -show_entries stream=start_time

它将返回如下内容:

Metadata:
major_brand     : isom
minor_version   : 512
compatible_brands: isomiso2avc1mp41
encoder         : Lavf58.45.100


Duration: 00:00:06.67, start: 0.000000, bitrate: 103 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 99 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
[STREAM]
start_time=0

在我的视频中,开始时间为 0,但在你的视频中,它可能有偏移 - 你可以使用偏移来获取正确的帧

相关内容