綾小路龍之介の素人思考

[ffmpeg] 動画ファイルから音声トラックを無劣化で取り出す

ffmpeg を利用して、フォーマットの確認、分離を行う。分離できるフォーマットは ffmpeg の対応による。

in.flv のフォーマットを確認。音声と動画トラックがひとつづつ、それぞれh264とaacコーデックで収められていることがわかる。

$ ffmpeg -i in.flv
FFmpeg version SVN-r0.5.9-4:0.5.9-1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.9-1 --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-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libfaad --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jun 10 2012 10:30:14, gcc: 4.4.5

Seems stream 0 codec frame rate differs from container frame rate: 59.83 (29917/500) -> 59.75 (239/4)
Input #0, flv, from 'in.flv':
  Duration: 00:04:12.96, start: 0.000000, bitrate: 400 kb/s
    Stream #0.0: Video: h264, yuv420p, 640x480 [PAR 1:1 DAR 4:3], 400 kb/s, 59.75 tbr, 1k tbn, 59.83 tbc
    Stream #0.1: Audio: aac, 44100 Hz, stereo, s16
At least one output file must be specified

音声トラックだけを無劣化で取り出すには以下のようにする。

$ ffmpeg -vn -acodec copy -i in.flv in.flv.aac
FFmpeg version SVN-r0.5.9-4:0.5.9-1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.9-1 --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-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libfaad --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jun 10 2012 10:30:14, gcc: 4.4.5

Seems stream 0 codec frame rate differs from container frame rate: 59.83 (29917/500) -> 59.75 (239/4)
Input #0, flv, from 'in.flv':
  Duration: 00:04:12.96, start: 0.000000, bitrate: 400 kb/s
    Stream #0.0: Video: h264, yuv420p, 640x480 [PAR 1:1 DAR 4:3], 400 kb/s, 59.75 tbr, 1k tbn, 59.83 tbc
    Stream #0.1: Audio: aac, 44100 Hz, stereo, s16
Output #0, adts, to 'in.flv.aac':
    Stream #0.0: Audio: 0x0000, 44100 Hz, stereo, s16
Stream mapping:
  Stream #0.1 -> #0.0
Press [q] to stop encoding
size=    3992kB time=252.94 bitrate= 129.3kbits/s
video:0kB audio:3918kB global headers:0kB muxing overhead 1.900754%

in.flv.aacのフォーマットを確認すると以下のようになる。

$ ffmpeg -i in.flv.aac
FFmpeg version SVN-r0.5.9-4:0.5.9-1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.9-1 --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-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libfaad --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jun 10 2012 10:30:14, gcc: 4.4.5
Input #0, aac, from 'in.flv.aac':
  Duration: 00:04:32.76, bitrate: 119 kb/s
    Stream #0.0: Audio: aac, 44100 Hz, stereo, s16, 119 kb/s
At least one output file must be specified
$ file in.flv.aac
in.flv.aac: MPEG ADTS, AAC, v4 LC, 44.1 kHz, stereo

ffmpeg は出力ファイルの拡張子から適当なヘッダをファイルにつける。トラックをcopyする場合この挙動が問題を生じる場合がある。例えばmp3という拡張子をつけた場合、音声トラックの抽出には成功するが、再生可能なファイルにはならない。これは元ファイルの音声トラックがaacだからである。

$ ffmpeg -vn -acodec copy -i in.flv in.flv.mp3
FFmpeg version SVN-r0.5.9-4:0.5.9-1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.9-1 --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-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libfaad --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jun 10 2012 10:30:14, gcc: 4.4.5

Seems stream 0 codec frame rate differs from container frame rate: 59.83 (29917/500) -> 59.75 (239/4)
Input #0, flv, from 'in.flv':
  Duration: 00:04:12.96, start: 0.000000, bitrate: 400 kb/s
    Stream #0.0: Video: h264, yuv420p, 640x480 [PAR 1:1 DAR 4:3], 400 kb/s, 59.75 tbr, 1k tbn, 59.83 tbc
    Stream #0.1: Audio: aac, 44100 Hz, stereo, s16
Output #0, mp3, to 'in.flv.mp3':
    Stream #0.0: Audio: 0x0000, 44100 Hz, stereo, s16
Stream mapping:
  Stream #0.1 -> #0.0
Press [q] to stop encoding
size=    3918kB time=252.94 bitrate= 126.9kbits/s
video:0kB audio:3918kB global headers:0kB muxing overhead 0.000798%
$ ffmpeg -i in.flv.mp3
FFmpeg version SVN-r0.5.9-4:0.5.9-1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.9-1 --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-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libfaad --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jun 10 2012 10:30:14, gcc: 4.4.5
[mp3 @ 0x9706a60]Could not find codec parameters (Audio: mp2, 0 channels, s16)
in.flv.mp3: could not find codec parameters
$ file in.flv.mp3
in.flv.mp3: Audio file with ID3 version 2.4.0, contains:

元ファイルの音声トラックと異なるフォーマットにするには、音声トラックのデコードと、再エンコードが必要である。in.flvの音声トラックをmp3にエンコードできるかどうかは、ffmpegがaacのデコードと、mp2へのエンコードが可能であることを確かめる必要がある。以下のようにして確かめられる。以下の結果からaacのデコードは可能だがmp3へのエンコードは不可能なことがわかる。

$ ffmpeg -formats | grep -e 'aac' -e 'mp3'
FFmpeg version SVN-r0.5.9-4:0.5.9-1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.9-1 --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-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libfaad --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jun 10 2012 10:30:14, gcc: 4.4.5
 D  aac             raw ADTS AAC
 DE mp3             MPEG audio layer 3
 D A    aac             Advanced Audio Coding
 D A    mp3             MP3 (MPEG audio layer 3)
 D A    mp3adu          ADU (Application Data Unit) MP3 (MPEG audio layer 3)
 D A    mp3on4          MP3onMP4
 text2movsub remove_extra noise mov2textsub mp3decomp mp3comp mjpegadump imxdump h264_mp4toannexb dump_extra

エンコードが可能なコーデックの例として、wmaがあげられる。

$ ffmpeg -formats | grep wma
FFmpeg version SVN-r0.5.9-4:0.5.9-1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.9-1 --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-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libfaad --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jun 10 2012 10:30:14, gcc: 4.4.5
 DEA    wmav1           Windows Media Audio 1
 DEA    wmav2           Windows Media Audio 2

無劣化ではなくなるがwmaでエンコードするには以下のようにする。エンコード中のメッセージを見ると、64kb/sのビットレートが使われていることがわかる。これに対して、無劣化の場合はaacで119kb/sのビットレートである。フォーマットが違うので一概に比較はできないが、かなりビットレートが落ちていることがわかる。聞き比べると明らかな劣化が感じられる。

$ ffmpeg -vn -i in.flv in.flv.wma
FFmpeg version SVN-r0.5.9-4:0.5.9-1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.9-1 --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-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libfaad --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jun 10 2012 10:30:14, gcc: 4.4.5

Seems stream 0 codec frame rate differs from container frame rate: 59.83 (29917/500) -> 59.75 (239/4)
Input #0, flv, from 'in.flv':
  Duration: 00:04:12.96, start: 0.000000, bitrate: 400 kb/s
    Stream #0.0: Video: h264, yuv420p, 640x480 [PAR 1:1 DAR 4:3], 400 kb/s, 59.75 tbr, 1k tbn, 59.83 tbc
    Stream #0.1: Audio: aac, 44100 Hz, stereo, s16
Output #0, asf, to 'in.flv.wma':
    Stream #0.0: Audio: mp2, 44100 Hz, stereo, s16, 64 kb/s
Stream mapping:
  Stream #0.1 -> #0.0
Press [q] to stop encoding
size=    2163kB time=252.89 bitrate=  70.1kbits/s
video:0kB audio:1976kB global headers:0kB muxing overhead 9.474770%

音声のビットレートを指定するには -ab オプションを利用するここでは128kb/sにしてみる。

$ ffmpeg -vn -ab 128k -i in.flv in.flv.wma
FFmpeg version SVN-r0.5.9-4:0.5.9-1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.9-1 --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-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libfaad --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jun 10 2012 10:30:14, gcc: 4.4.5

Seems stream 0 codec frame rate differs from container frame rate: 59.83 (29917/500) -> 59.75 (239/4)
Input #0, flv, from 'in.flv':
  Duration: 00:04:12.96, start: 0.000000, bitrate: 400 kb/s
    Stream #0.0: Video: h264, yuv420p, 640x480 [PAR 1:1 DAR 4:3], 400 kb/s, 59.75 tbr, 1k tbn, 59.83 tbc
    Stream #0.1: Audio: aac, 44100 Hz, stereo, s16
File 'in.flv.wma' already exists. Overwrite ? [y/N] y
Output #0, asf, to 'in.flv.wma':
    Stream #0.0: Audio: mp2, 44100 Hz, stereo, s16, 128 kb/s
Stream mapping:
  Stream #0.1 -> #0.0
Press [q] to stop encoding
size=    4322kB time=252.89 bitrate= 140.0kbits/s
video:0kB audio:3951kB global headers:0kB muxing overhead 9.385354%
$ ffmpeg -i in.flv.wma
FFmpeg version SVN-r0.5.9-4:0.5.9-1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.9-1 --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-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc --enable-swscale --enable-x11grab --enable-libfaad --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jun 10 2012 10:30:14, gcc: 4.4.5
Input #0, asf, from 'in.flv.wma':
  Duration: 00:04:09.79, start: 3.100000, bitrate: 141 kb/s
    Stream #0.0: Audio: mp2, 44100 Hz, stereo, s16, 128 kb/s
At least one output file must be specified
$ file in.flv.wma
in.flv.wma: Microsoft ASF

リファレンス

  1. ついでに動画ファイルフォーマットやffmpegとの関係を纏めてみた - eggshell blue
  2. [雑資料] 動画メモ(オプションとか) | anobota
  3. aki-初心者向けYoutubeとFLV/MP4: Youtube(FLVファイル)をMP3に変換(FFMpegを使う)

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2010-07-01T11:19:05+09:00
  2. Modified: 2010-07-01T11:19:05+09:00
  3. Generated: 2017-04-17T23:09:41+09:00