ffmpeg:管道輸入錯誤

ffmpeg:管道輸入錯誤

當輸入和輸出是管道時,我在使用 ffmpeg 時遇到問題。儘管輸出管道工作正常,但輸入管道會導致一些錯誤。

我嘗試使用以下命令將 mp4 視訊轉換為 flv,效果很好:

$ ffmpeg  -i video-2012-04-26-19-48-40.mp4 -ar 44100 -ab 96 -f flv pipe:1 | cat> videoname.flv

它工作得很好並給出了以下輸出:

FFmpeg version 0.6-4:0.6-2ubuntu6.3, Copyright (c) 2000-2010 the FFmpeg developers
  built on Dec 21 2011 18:37:43 with gcc 4.4.5
  configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --enable-shared --disable-static
  WARNING: library configuration mismatch
  libavutil   configuration: --extra-version=4:0.6-2ubuntu3.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libdirac --enable-libgsm --enable-libopenjpeg --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-libmp3lame --enable-gpl --enable-postproc --enable-x11grab --enable-libfaad --enable-libxvid --enable-libx264 --enable-librtmp --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libavcodec  configuration: --extra-version=4:0.6-2ubuntu3.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libdirac --enable-libgsm --enable-libopenjpeg --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-libmp3lame --enable-gpl --enable-postproc --enable-x11grab --enable-libfaad --enable-libxvid --enable-libx264 --enable-librtmp --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libavformat configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libavdevice configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libavfilter configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libswscale  configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libpostproc configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libavutil     50.15. 1 / 50.15. 1
  libavcodec    52.72. 2 / 52.72. 2
  libavformat   52.64. 2 / 52.64. 2
  libavdevice   52. 2. 0 / 52. 2. 0
  libavfilter    1.19. 0 /  1.19. 0
  libswscale     0.11. 0 /  0.11. 0
  libpostproc   51. 2. 0 / 51. 2. 0

Seems stream 0 codec frame rate differs from container frame rate: 60000.00 (60000/1) -> 1000.00 (1000/1)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video-2012-04-26-19-48-40.mp4':
  Metadata:
    major_brand     : 3gp4
    minor_version   : 768
    compatible_brands: 3gp43gp6
  Duration: 00:00:09.55, start: 0.000000, bitrate: 3366 kb/s
    Stream #0.0(eng): Video: h264, yuv420p, 720x480, 3304 kb/s, 29.61 fps, 1k tbr, 30k tbn, 60k tbc
    Stream #0.1(eng): Audio: aac, 16000 Hz, mono, s16, 56 kb/s
WARNING: The bitrate parameter is set too low. It takes bits/s as argument, not kbits/s
Output #0, flv, to 'pipe:1':
  Metadata:
    encoder         : Lavf52.64.2
    Stream #0.0(eng): Video: flv, yuv420p, 720x480, q=2-31, 200 kb/s, 1k tbn, 1k tbc
    Stream #0.1(eng): Audio: adpcm_swf, 44100 Hz, mono, s16, 0 kb/s
Stream mapping:
  Stream #0.0 -> #0.0
  Stream #0.1 -> #0.1
Press [q] to stop encoding
frame=  283 fps=175 q=31.0 Lsize=     924kB time=9.52 bitrate= 794.7kbits/s    
video:709kB audio:207kB global headers:0kB muxing overhead 0.867656%

但是當我也從管道讀取輸入時,出現錯誤:

命令:

$cat video-2012-04-26-19-48-40.mp4| ffmpeg -i pipe: -ar 44100 -ab 96 -f flv pipe:1 | cat> videoname.flv

錯誤輸出:

FFmpeg version 0.6-4:0.6-2ubuntu6.3, Copyright (c) 2000-2010 the FFmpeg developers
  built on Dec 21 2011 18:37:43 with gcc 4.4.5
  configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --enable-shared --disable-static
  WARNING: library configuration mismatch
  libavutil   configuration: --extra-version=4:0.6-2ubuntu3.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libdirac --enable-libgsm --enable-libopenjpeg --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-libmp3lame --enable-gpl --enable-postproc --enable-x11grab --enable-libfaad --enable-libxvid --enable-libx264 --enable-librtmp --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libavcodec  configuration: --extra-version=4:0.6-2ubuntu3.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libdirac --enable-libgsm --enable-libopenjpeg --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-libmp3lame --enable-gpl --enable-postproc --enable-x11grab --enable-libfaad --enable-libxvid --enable-libx264 --enable-librtmp --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libavformat configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libavdevice configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libavfilter configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libswscale  configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libpostproc configuration: --extra-version=4:0.6-2ubuntu6.3 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libvorbis --enable-vaapi --enable-pthreads --enable-zlib --enable-libvpx --disable-stripping --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-x11grab --enable-libdc1394 --shlibdir=/usr/lib/i686/cmov --cpu=i686 --enable-shared --disable-static --disable-ffmpeg --disable-ffplay
  libavutil     50.15. 1 / 50.15. 1
  libavcodec    52.72. 2 / 52.72. 2
  libavformat   52.64. 2 / 52.64. 2
  libavdevice   52. 2. 0 / 52. 2. 0
  libavfilter    1.19. 0 /  1.19. 0
  libswscale     0.11. 0 /  0.11. 0
  libpostproc   51. 2. 0 / 51. 2. 0
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x8962bc0]stream 0, offset 0x28: partial file
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x8962bc0]Could not find codec parameters (Video: h264, 3304 kb/s)
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x8962bc0]Could not find codec parameters (Audio: aac, mono, s16, 56 kb/s)
pipe:: could not find codec parameters

請幫我修復錯誤。

答案1

簡短回答:

跑步

mp4box -hint

在管道之前在 MP4 檔案上,以便結構元資料(「moov」框)位於檔案的開頭附近。或者您可以設計另一種解決方案來重新排列管道中的 mp4 流 - 儘管這可能需要緩衝整個影片。

長答案:

moov當我透過管道傳輸一個框位於框後面的 mp4 檔案時,我在 FFmpeg 中看到相同的錯誤mdat。許多 mp4 產生器都會產生此類文件,因為它在錄製時最方便,但對於消費(尤其是在串流和管道傳輸時),通常最好安排文件以便盒子moov出現盒子mdat

moov在您的場景中,FFmpeg在開始處理視訊幀之前會掃描串流以查找元資料。在解析moov文件末尾的元資料後,它會嘗試返回到mdat開頭附近的資料。當然,您無法在管道輸入中找到。 FFmpeg 的 I/O 抽象層(「avio」)確實提供了在記憶體中維護緩衝區的有限功能,並允許在該緩衝區內進行查找,但緩衝區不夠大,無法容納整個mdat框(即所有視訊)幀!

您可能會考慮的一些選擇:

  1. 在管道傳輸之前,在 mp4 文件上運行 MP4Box(使用 -hint 參數),以重新排列 mp4 文件,以便該moov框位於文件的開頭。
  2. 也許 FFmpeg 的 avio 緩衝區可以增加到可以容納您預期處理的最大視訊檔案的大小。然後,「尋回盒子mdat」將會成功,因為緩衝區包含整個檔案。顯然,這對於記憶體來說是非常浪費的。
  3. 如果您絕對需要管道解決方案,您可以設計一個程式來接受標準輸入上的 mdat-before-moov mp4,並將翻譯後的 moov-before-mdat 流寫入標準輸出。這還需要緩衝整個文件,這可以在記憶體或臨時文件中完成。

答案2

奇怪的是 David 沒有建議qt-faststart這樣做,因為他顯然是 FFmpeg 的粉絲,而且它包含在原始碼中。

從 FFmpeg 來源目錄:

./configure --disable-everything #(only needed if you haven't built ffmpeg already) 
make tools/qt-faststart
chmod +x tools/qt-faststart
sudo cp tools/qt-faststart /usr/local/bin #(optional step to move the binary to a PATH included directory)

然後,您可以使用它將 moov 原子移至檔案的開頭,在本例中:

qt-faststart video-2012-04-26-19-48-40.mp4 video-out-file.mp4

相關內容