从flv文件中提取音频并存储为mp3格式

来源:互联网 发布:廖常初plc编程及应用 编辑:程序博客网 时间:2024/06/01 13:41

在优酷上下载了一些教学视频,为了把它们放到mp3里面,需要从这些flv文件中提取出音频数据并存储为mp3格式。

操作系统是centos 5.3。

在网上搜索了一下,找到以下方法:
方法一:使用mencoder,例如:mencoder -of avi -nosound -ovc copy in.flv -o out_just_vid.avi
方法二:使用mplayer,例如:mplayer -dumpaudio nodame_theme.flv -dumpfile nodame_theme.mp3
方法三:使用ffmpeg。
以上三种方法,可参看:
http://linux.byexamples.com/archives/229/extract-audio-from-video-or-online-stream/
http://www.linuxquestions.org/questions/linux-desktop-74/copy-mode-extract-of-audio-from-youtube-flv-video-508026/

这里使用功能非常强大的ffmpeg来完成这项任务。

ffmpeg简介

ffmpeg的首页:http://www.ffmpeg.org/index.html
FFmpeg is a complete, cross-platform solution to record, convert andstream audio and video. It includes libavcodec - the leading audio/videocodec library.

ffmpeg安装

从http://www.ffmpeg.org/releases/ffmpeg-checkout-snapshot.tar.bz2下载ffmpeg的源代码。解压后进入源代码目录,三步走(configuer, make, make install)来安装它,使用./configure--help看具体的安装配置选项。

使用ffmpeg从flv文件中提取音频并转换

首先查看flv文件的信息:
[whb@jcwkyl introduction_to_algorithm]$ ffmpeg -i Lecture_1.flv
FFmpeg version SVN-r21915, Copyright (c) 2000-2010 the FFmpeg developers
  built on Feb 20 2010 18:28:18 with gcc 4.1.2 20071124 (Red Hat 4.1.2-42)
  configuration: --enable-encoder=mp3
  libavutil     50. 9. 0 / 50. 9. 0
  libavcodec    52.54. 0 / 52.54. 0
  libavformat   52.52. 0 / 52.52. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libswscale     0.10. 0 /  0.10. 0
[flv @ 0x8a533a0]Estimating duration from bitrate, this may be inaccurate

Seems stream 0 codec frame rate differs from container frame rate: 2000.00 (2000/1) -> 15.00 (15/1)
Input #0, flv, from 'Lecture_1.flv':
  Metadata:
    duration        : 4835
    starttime       : 0
    totalduration   : 4835
    width           : 320
    height          : 240
    videodatarate   : 196
    audiodatarate   : 63
    totaldatarate   : 264
    framerate       : 15
    bytelength      : 159348112
    canseekontime   : true
    sourcedata      : CiaMM
    purl            :
    pmsg            :
  Duration: 01:20:35.06, start: 0.000000, bitrate: 200 kb/s
    Stream #0.0: Video: h264, yuv420p, 320x240 [PAR 1:1 DAR 4:3], 200 kb/s, 15 tbr, 1k tbn, 2k tbc
    Stream #0.1: Audio: aac, 22050 Hz, stereo, s16
At least one output file must be specified

上面输出中用红色标记的两行显示出这个文件的视频和音频数据的各种属性,其中音频数据使用的是aac编码,这里需要把音频转换成mp3格式,所以需要aac的解码器和mp3的编码器。
查看ffmpeg是否有这些工具。
[whb@jcwkyl introduction_to_algorithm]$ ffmpeg -codecs | grep aac
FFmpeg version SVN-r21915, Copyright (c) 2000-2010 the FFmpeg developers
  built on Feb 20 2010 18:28:18 with gcc 4.1.2 20071124 (Red Hat 4.1.2-42)
  configuration: --enable-encoder=mp3
  libavutil     50. 9. 0 / 50. 9. 0
  libavcodec    52.54. 0 / 52.54. 0
  libavformat   52.52. 0 / 52.52. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libswscale     0.10. 0 /  0.10. 0
 DEA    aac             Advanced Audio Coding
D表示decoder,E表示encoder,A表示Audio Codec。aac的编码解码器都存在。
再来查看是否提供了mp3的编码器,同样使用命令:ffmpeg -codecs,在输出结果中发现只有mp2的编码器,而没有mp3编码器:
[whb@jcwkyl introduction_to_algorithm]$ ffmpeg -codecs
......
 D A    mp1             MP1 (MPEG audio layer 1)
 DEA    mp2             MP2 (MPEG audio layer 2)
 D A    mp3             MP3 (MPEG audio layer 3)
......
所以先试着使用mp2编码器进行转换:
[whb@jcwkyl introduction_to_algorithm]$ ffmpeg -i Lecture_1.flv -f mp2 -vn Lecture_1.mp3
这条命令中,-i表示input file,-f表示输出格式,-vn表示“vedio not",即禁止视频输出,最后转换后的文件是Lecture_1.mp3。
转换完成后,使用file命令查看Lecture_1.mp3的文件格式:
[whb@jcwkyl introduction_to_algorithm]$ file Lecture_1.mp3
Lecture_1.mp3: MPEG ADTS, layer II, v2,  64 kBits, 22.05 kHz, Stereo
转换前后文件大小对比:
[whb@jcwkyl introduction_to_algorithm]$ du -hs Lecture_1.*
153M    Lecture_1.flv
37M     Lecture_1.mp3

使用播放器播放Lecture_1.mp3,完全正常。

原创粉丝点击