Use FFmpeg to extract audio (sound) from video and save it as mp3 file

Use FFmpeg to extract audio (sound) from video and save as mp3 file



Use FFmpeg to extract audio (sound) from video and save as mp3 file
Author: Rain, Date: January 9, 2016 CSDN Blog: http://blog.csdn .net/gobitan

summary: If you see a good video file, if you want to extract the audio (sound) from it. There must be many similar tools on the Internet, but as a technician, when it comes to audio and video processing, FFmpeg will definitely come to mind. This article describes how to extract audio from video based on FFmpeg, and then save the audio as an audio file in mp3 format.

Environment preparation: Ubuntu desktop 12.04

Step 1: Download the mp3 encoding library
because FFmpeg only supports mp3 decoding by default, and does not support mp3 encoding. If you want to save the extracted audio in mp3 format, you will definitely need an encoding library in mp3 format. Therefore, if you want to output mp3 files, you need to use a third-party mp3 encoding library. The LAME encoding library is used here, namely Lame Aint an MP3 Encoder (A high quality MP3 encoder). The LAME library can be downloaded from http://lame.sourceforge.net/. The version I chose when I downloaded it here is 3.98, and the file name is lame-398.tar.gz, which is about 1M. For the selection of the specific version, refer to the prompts when compiling ffmpeg later. Visit the download, which can be found here http://download.csdn.net/detail/gobitan/9397485.

Step 2: Unzip/configure/compile/install LAME mp3 encoding
library Perform the following steps to complete the installation:
dennis@ubuntu14:~$ tar zxf lame-398.tar.gz
dennis@ubuntu14:~$ cd lame-398/
dennis@ubuntu14:~/lame-398$ ./configure
dennis@ubuntu14:~/lame-398$ make
dennis@ubuntu14:~/lame-398$ sudo make install

third Step: Download the FFmpeg installation package
The FFmpeg installation package can be downloaded from http://ffmpeg.org/. The latest version is 2.8.4. The downloaded file is ffmpeg-2.8.4.tar.bz2, about 8M.

Step 4: Unzip/configure/compile/install FFmpeg
Upload the downloaded installation package to ubuntu, and then do the following steps:
(1) Unzip
dennis@ubuntu:~$ tar xf ffmpeg-2.8.4.tar.bz2
dennis@ubuntu :~$ cd ffmpeg-2.8.4/
(2) Configuration
Since the third-party mp3 support library needs to be loaded, a configuration option --enable-libmp3lame needs to be added during configuration. The query of configuration options can be viewed through the help command. As follows:
dennis@ubuntu:~/ffmpeg-2.8.4$ ./configure --help|grep mp3
  --enable-libmp3lame enable MP3 encoding via libmp3lame [no]
Execute the following command to configure:
dennis@ubuntu14:~/ffmpeg-2.8.4$ ./configure --enable-libmp3lame --disable-yasm
If the LAME mp3 library in the first step is not installed, an error similar to the following will appear:
ERROR: libmp3lame >= 3.98.3 not found
Note: The version of the LAME library that matches ffmpeg can be provided above.
(3) Compile
dennis@ubuntu:~/ffmpeg-2.8.4$ make
(4) Install
dennis@ubuntu:~/ffmpeg-2.8.4$ sudo make install
(5) Configure shared library
dennis@ubuntu:~$ sudo vi /etc/ld.so.conf
dennis@ubuntu:~$ sudo ldconfig -v
If this step is missing, the following error will be reported when extracting later:
ffmpeg: error while loading shared libraries: libmp3lame.so.0: cannot open shared object file: No such file or directory

Step 4: Find a video file that can be tested. Here
I downloaded the video of Little Apple from Beva Erge.com, URL: http://g.beva.com/kan-erge/xiao- ping-guo.html#1802, about 23M. Rename the file to apple.mp4.

Step 5: Perform audio extraction
dennis@ubuntu:~$ ffmpeg -i apple.mp4 -f mp3 -vn apple.mp3
parameter explanation:
-i means input, that is, the input file
-f means format, that is, the output format
-vn means vedio not, that is, the output does not contain video
Comparing the size of the source video file and the extracted audio file, it can be seen that the size of the source video file is about 23M, while the size of the extracted audio file is 3M.
dennis@ubuntu:~$ ls -lrt
-rw-rw-r-- 1 dennis dennis 24118025 Jan 9 02:52 apple.mp4
-rw-rw-r-- 1 dennis dennis 3379969 Jan 9 02:54 apple.mp3
dennis @ubuntu:~$

Step 6: Extended Information
FFmpeg also provides many useful tools to view and process audio and video files, such as:
viewing the audio and video codec format, video duration, bit rate, etc. of the video file, as follows:
dennis@ ubuntu:~$ ffmpeg -i apple.mp4
ffmpeg version 2.8.4 Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
  configuration: --enable-libmp3lame --disable- yasm
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'apple.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf54.6.100
  Duration: 00:03:31.24, start: 0.000000, bitrate: 913 kb/s
    Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 780 kb/s, 25 fps, 25 tbr, 25 tbn, 25 tbc (default)
    Metadata:
      handler_name : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s (default)
    Metadata:
      handler_name : SoundHandler
At least one output file must be specified
dennis@ubuntu:~$

References:
1. http://ffmpeg.org/ FFmpeg official website
2. http://lame.sourceforge.net/ LAME mp3 codec official website
3. http: //blog.csdn.net/jcwkyl/article/details/5313297 Extract audio from flv file and store as mp3 format
4. http://blog.chinaunix.net/uid-11344913-id-3930867.html Compilation supports MP3 ffmpeg encoded with H264

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326400283&siteId=291194637