Transcoding an audio or a video file
You want to play a movie extracted from a DVD on your phone, copy your CD collection on your PC, watch a program downloaded on the internet on your TV or broadcast a home video on the web? Learn to master the art of converting audio and video formats.
Introduction
The standard open-source programs for transforming audio and video files are ffmpeg and mplayer. Each is capable of reading and writing audios and videos in many different formats using many different options. mplayer has more filters than ffmpeg for processing a video and sound tracks. ffmpeg supports more input and output formats, including reading WMA files from Windows and writing MP4 and 3GP containers used in mobile phones. Both have so many parameters that searching for the right ones to obtain a particular target can be daunting. This article will let you find the right command and give you the confidence to go further.
Patent and copyright laws operate differently depending on which country you are in. Please obtain legal advice if you are unsure whether a particular patent or restriction applies to a media format you wish to use in your country.
Installation
Linux
Install the codecs:
$ sudo apt install ubuntu-restricted-extras
To read a DVD:
$ sudo add-apt-repository multiverse
$ sudo apt install libdvd-pkg
$ sudo dpkg-reconfigure libdvd-pkg
NOTE: The library ibdvdcss2
from VLC repository is also installed.
Install mencoder with mplayer:
$ sudo apt install mplayer mencoder
Add tools to create and manipulate MP3 or Vorbis audio files, OGM media streams and Matroska and MP4 containers:
$ sudo apt install lame vorbis-tools ogmtools mkvtoolnix mkvtoolnix-gui gpac
Install ffmpeg with the libraries for encoding the audio in AAC and the video in H.264:
$ sudo apt install ffmpeg
Check if an audio can be encoded in AAC:
$ ffmpeg -formats | grep -i aac
D aac raw ADTS AAC (Advanced Audio Coding)
E adts ADTS AAC (Advanced Audio Coding)
The last line with an E tells us that ffmpeg is compiled with the library libfdk_aac
.
NOTE: If the command doesn't list any AAC library, try replacing -formats
by -codecs
:
$ ffmpeg -codecs | grep -i aac
DEA.L. aac AAC (Advanced Audio Coding) (decoders: aac aac_fixed )
D.A.L. aac_latm AAC LATM (Advanced Audio Coding LATM syntax)
Also check if a video can be encoded in H.264:
$ ffmpeg -formats | grep 264
DE h264 raw H.264 video
E ipod iPod H.264 MP4 (MPEG-4 Part 14)
We'll use libx264 to encode a video stream in H.264.
NOTE: If the command doesn't list libx264
, try replacing -formats
by -codecs
:
$ ffmpeg -codecs | grep 264
DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_v4l2m2m h264_cuvid ) (encoders: libx264 libx264rgb h264_nvenc h264_omx h264_v4l2m2m h264_vaapi nvenc nvenc_h264 )
Windows
Download and install the latest K-Lite Codec Pack.
Create a folder called ffmpeg in C:\Program Files.
Go to the site Zeranoe FFmpeg builds. Download the latest 32 bits or 64 bits archive in C:\Program Files\ffmpeg and extract its contents with 7zip. Download MPlayer and MEncoder builds for Windows and copy the programs in C:\Program Files\ffmpeg\bin.
The commands ffmpeg
, ffplay
, mplayer
and mencoder
are in C:\Program Files\ffmpeg\bin.
Start the command processor and add this directory to the PATH system variable:
C:\> set PATH=%PATH%;"C:\Program Files\ffmpeg\bin"
C:\> cd C:\Documents and Settings\frasq\My documents\My videos
C:\Documents and Settings\frasq\My documents\My videos\> ffmpeg -version
Alternatives
SDL
SDL (Simple DirectMedia Layer) is a library which gives low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer. It's used by many game developers. It's at the core of mplayer and ffplay.
To install the version of the system:
$ sudo apt install libsdl2-dev
To build the package, download the source code here and type the following commands:
$ cd SDL2-2.0.14
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
MP3
To install the version of the system:
$ sudo apt install libmp3lame-dev
To build the package, download lame and type the following commands:
$ cd lame-3.99.5
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
AAC
To install the version of the system:
$ sudo apt install libfaac-dev
To build the package, download faac and type the following commands:
$ cd faac-1.28
$ ./configure --prefix=/usr/local --disable-thorough-tests
$ make
$ sudo make install
The AAC library provided by the Fraunhofer is more recent. To build the package, type the following commands:
$ git clone git://github.com/mstorsjo/fdk-aac.git
$ cd fdk-aac
$ autoreconf -fiv
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
If necessary, try the following command to install libtoolize
:
$ sudo apt install libtool
FLAC
To install the version of the system:
$ sudo apt install flac
To build the package, download libflac, then type the following commands:
$ cd libflac
$ ./configure --prefix=/usr/local --disable-thorough-tests
$ make
$ sudo make install
Ogg Vorbis
To install the version of the system:
$ sudo apt install libogg-dev libvorbis-dev
To build the package, download libogg and libvorbis, then type the following commands:
$ cd libogg-1.3.5
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
$ cd libvorbis-1.3.7
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
AMR
To install the versions of the system of the decoder and the encoder:
$ sudo apt install libopencore-amrnb-dev libopencore-amrwb-dev libvo-amrwbenc-dev
To install the latest versions, download opencore-amr and vo-amrwbenc, then type the following commands:
$ cd opencore-amr-0.1.5
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
$ cd vo-amrwbenc-0.1.3
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
Opus
To install the versions of the system of the decoder and the encoder:
$ sudo apt install opus-tools
VMAF
To download and install the VMAF library from Netflix:
$ sudo apt install meson
$ sudo apt install doxygen
$ sudo apt install python-pip3
$ sudo pip3 install cython
$ sudo pip3 install numpy
$ git clone https://github.com/Netflix/vmaf.git
$ cd vmaf
$ make
$ sudo make install
NOTE: After a git pull
, enter make clean
.
H.264
To install the version of the system:
$ sudo apt install libx264-dev
Make sure Yasm and Nasm are installed:
$ sudo apt install yasm nasm
To download and build Yasm, type the following commands:
$ git clone git://github.com/yasm/yasm.git
$ cd yasm
$ ./autogen.sh
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
If generating configure
fails, install autoconf
:
$ sudo apt install autoconf
To download and build Nasm, type the following commands:
$ wget http://www.nasm.us/pub/nasm/releasebuilds/2.13.01/nasm-2.13.01.tar.bz2
$ tar -xf nasm-2.13.01.tar.bz2
$ cd nasm-2.13.01
$ ./autogen.sh
$ ./configure --prefix=/usr/local/
$ make
$ sudo make install
To download and build the package, type the following commands:
$ git clone https://code.videolan.org/videolan/x264.git x264
$ cd x264
$ ./configure --prefix=/usr/local --enable-static --enable-shared
$ make
$ sudo make install install-lib-static
NOTE: In order to compile the executable x264
, make sure you have installed the latest libraries which are delivered with FFmpeg.
If you don't want to compile the executable, add the option --disable-cli
to configure
.
H.265
To install the version of the system:
$ sudo apt install libx265-dev
Make sure the commands cmake
and ccmake
are installed:
$ sudo apt install cmake cmake-curses-gui
To download and build the package, type the following commands:
$ git clone https://github.com/videolan/x265
$ cd x265
$ cmake source
$ make
$ sudo make install
VP8 - VP9
To download and build the package, type the following commands:
$ git clone https://chromium.googlesource.com/webm/libvpx libvpx
$ cd libvpx
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
Xvid
To install the version of the system:
$ sudo apt install libxvidcore-dev
To build the package, download xvidcore and type the following commands:
$ cd xvidcore/build/generic
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
MKV
To install the version of the system:
$ sudo apt install libebml-dev libmatroska-dev
To build the packages, download libebml and libmatroska then type the following commands:
$ cd libebml-1.4.2
$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install
$ cd libmatroska-1.6.3
$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install
FFmpeg
Make sure the system linker looks for libraries in /usr/local/lib by adding the following lines to /etc/ld.so.conf.d/libc.conf:
# libc default configuration
/usr/local/lib
Remember to run ldconfig
every time you install a file in /usr/local/lib:
$ sudo ldconfig
Make sure the X11 library is installed:
$ sudo apt install libX11-dev
To download and build the package, type the following commands:
$ git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
$ cd ffmpeg
$ ./configure --prefix=/usr/local \
--enable-gpl --enable-version3 --enable-nonfree \
--enable-postproc --enable-pthreads \
--enable-libfdk_aac --enable-libmp3lame --enable-libvorbis \
--enable-libvpx --enable-libx264 --enable-libxvid \
--enable-ffplay
$ make clean
$ make all
$ sudo make install
Installs ffmpeg
, ffplay
, ffprobe
and ffserver
.
Add the option --enable-x265
to add the H.265 encoder, the option --enable-libvmaf
for the VMAF encoder.
To generate AMR NB or AMR WB files, add the options --enable-libopencore_amrnb --enable-libvo-amrwbenc
.
To build ffmpeg
for a server, enable fewer options:
$ ./configure --prefix=/usr/local \
--enable-gpl --enable-version3 --enable-nonfree \
--enable-postproc --enable-pthreads \
--enable-libfdk_aac --enable-libmp3lame --enable-libvorbis --enable-libx264
To update ffmpeg
:
$ make clean
$ git pull
Finish with ./configure && make && sudo make install
.
MPlayer
Download the source code:
$ svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer
Don't forget to install libdvdread-dev
:
$ sudo apt install libdvdread-dev
Configure with the default options:
$ ./configure --prefix=/usr/local
NOTE: Let the configurator download a copy of the code for FFmpeg.
If necessary, install the library zlib for developers with sudo apt install zlib1g-dev
.
Check the list of the optional drivers which are enabled.
Compile and install the commands mplayer
and mencoder
:
$ make clean
$ make all
$ sudo make install
IMPORTANT: Put the directory /usr/local/bin before /usr/bin in the PATH variable and /usr/local/lib before /usr/lib in the LD_LIBRARY_PATH variable or uninstall the official versions from the system.
Playing a DVD
Start by locating the video you want on the DVD:
$ mplayer dvd://1
...
There are 5 titles on this DVD.
...
audio stream: 0 format: ac3 (stereo) language: en aid: 128.
audio stream: 1 format: ac3 (stereo) language: fr aid: 129.
number of audio channels on disk: 2.
subtitle ( sid ): 0 language: en
subtitle ( sid ): 1 language: fr
number of subtitles on disk: 2
...
VIDEO: MPEG2 720x576 (aspect 2) 25.000 fps 7500.0 kbps (937.5 kbyte/s)
...
Selected video codec: [ffmpeg2] vfm: ffmpeg (FFmpeg MPEG-2)
...
AUDIO: 48000 Hz, 2 ch, s16le, 192.0 kbit/12.50% (ratio: 24000->192000)
...
Selected audio codec: [a52] afm: liba52 (AC3-liba52)
...
Movie-Aspect is 1.33:1 - prescaling to correct movie aspect.
...
To quit the player, press q.
The information printed out by the program is quite rich. Add -identify
for an even more complete output. Write down the number of titles in the DVD, the language of each sound track with its id, its codec, the number of subtitle channels if any, the frame rate of the video, its codec, its size and its aspect ratio.
To read an ISO file:
$ mplayer -dvd-device file.iso dvd://1
To generate an ISO file from a directory:
$ genisoimage -iso-level 1 -dvd-video -volset-size 1 -o dvd.iso dvddir
To play the second title:
$ mplayer dvd://2
To list the chapters of the third title:
$ dvdxchap -t 3 /dev/dvd
To play the first 3 chapters of the second title:
$ mplayer -chapter 1-3 dvd://2
Press @ to move to the next chapter. Press ! to move to the previous chapter.
Extracting a DVD
To extract a particular title in VOB:
$ mplayer -dumpstream dvd://1 -dumpfile video.vob
To extract just some chapters:
$ mplayer -chapter 2-5 -dumpstream dvd://1 -dumpfile video.vob
To combine several VOB files in one under Linux:
$ cat VTS_01_1.VOB VTS_01_2.VOB VTS_01_3.VOB > video.vob
Under Windows:
C:\> copy /b VTS_01_1.VOB + VTS_01_2.VOB + VTS_01_3.VOB video.vob
To find the sound tracks:
$ mplayer video.vob -v -vo null -ao null -frames 0 | grep "audio stream:"
==> Found audio stream: 128
To play the video with one of the sound tracks:
$ mplayer video.vob -aid 128
To choose the subtitles:
$ mplayer video.vob -aid 128 -sid 1
To extract the sound track in WAV:
$ mplayer video.vob -aid 128 -ao pcm:file=audio.wav -ao pcm:fast -vc null -vo null
To listen to the sound track:
$ mplayer audio.wav
To encode the sound track in MP3:
$ lame -V 6 audio.wav audio.mp3
Add the option --scale 2
to double the volume.
To extract subtitles in a VOBSUB file:
$ rm subtitles.idx subtitles.sub
$ mencoder video.vob -vobsubout subtitles -vobsuboutindex 0 -vobsuboutid en -sid 0 -o /dev/null -nosound -ovc frameno
$ mencoder video.vob -vobsubout subtitles -vobsuboutindex 1 -vobsuboutid fr -sid 1 -o /dev/null -nosound -ovc frameno
Each successive command adds a subtitle stream to the files subtitles.sub and subtitles.idx.
To display a video with subtitles from a VOBSUB file:
$ mplayer video.vob -noautosub -vobsub subtitles -vobsubid 1
The option -vobsubid 1
selects the subtitle stream with index 1.
To convert a VOBSUB in SRT, install tesseract
and vobsub2srt
:
$ sudo apt-get install tesseract-ocr tesseract-eng tesseract-fra vobsub2srt
To generate the file subtitles.srt from the files subtitles.idx and subtitles.sub:
$ vobsub2srt --lang en subtitles
If the generated file contains no text, try to specify the color of the subtitles by adding the following lines to the file subtitles.idx:
palette: 000000, 828282, 828282, 828282, 828282, 828282, 828282, ffffff, 828282, bababa, 828282, 828282, 828282, 828282, 828282, 828282
custom colors: OFF, tridx: 1000, colors: 000000, bababa, 828282, 000000
NOTE: Use VobSub
under Windows for a more interactive and more precise approach.
To play a video with a smaller display:
$ mplayer video.vob -vf scale=360:288 -nosound
When resizing a video, preserve its aspect by dividing both dimensions by the same factor.
If playing the video shows black stripes, find out the size of the video without them:
$ mplayer video.vob -nosound -vo null -vf cropdetect
...
[CROP] Crop area: X: 4..711 Y: 0..575 (-vf crop=704:576:6:0).
...
Press q to quit the player as soon as the output is stable.
Play the video while cropping the display:
$ mplayer video.vob -vf crop=704:576:6:0 -nosound
To limit the size of the video, compute the bitrate with the following formular:
bitrate=(file_size_in_mb - audio_size_in_mb) * 1024 * 1024 / length_in_secs * 8 / 1000
To obtain a 702MB file for a 2 hour long movie with a 60MB audio file, the bitrate is
(702 - 60) * 1024 * 1024 / (120 * 60) * 8 / 1000 = 747kbps
Alternatively, you can more simply pass the size of the video in bytes as a negative argument to the parameter bitrate:
bitrate=-642000
Encode the video in H.264 in two steps:
$ mencoder video.vob -o video.avi -nosound -nosub -ovc x264 -x264encopts \
bitrate=800:frameref=8:mixed_refs:bframes=3:subq=3:weight_b:pass=1
$ mencoder video.vob -o video.avi -nosound -nosub -ovc x264 -x264encopts \
bitrate=800:frameref=8:mixed_refs:bframes=3:b_adapt:weight_b:partitions=all:8x8dct:me=umh:subq=7:trellis=2:pass=2
NOTE: Some parameters may vary depending on the version of the encoder.
The statistics of the video are recorded during the first pass into a log file. The second pass uses this log file to generate the video at the exact requested bitrate.
Adapt the bitrate
parameter to the length of the video. To resize the video, add -vf scale=w:h
.
To crop the video, add -vf crop=w:h:x:y
.
If you crop the video, you will probably want to rescale it to its original ratio.
For example, the option -vf crop=704:480:8:0,scale=720:480
will remove an 8 pixels black stripe on the left and the right of the video and resize it to its original format of 720x480 pixels.
Play the video:
$ mplayer video.avi
...
VIDEO: [h264] 720x576 24bpp 25.000 fps 799.9 kbps (97.6 kbyte/s)
...
To overlay subtitles:
$ mencoder video.avi -nosound -ovc x264 \
-sub video.srt -utf8 -ffactor 1 -fontconfig -font Arial -subfont-text-scale 3 -o videosub.avi
To delay the subtitles, add the option -subdelay sec
where sec
is a number of seconds. sec
can be negative.
To combine the video and the audio:
$ mencoder video.avi -audiofile audio.mp3 -ovc copy -oac copy -o movie.avi
Encode the video in XVID for a DVD player:
$ mencoder video.vob -o divx.avi -ovc xvid -xvidencopts vhq=4:turbo:pass=1 -nosound
$ mencoder video.vob -o divx.avi -ovc xvid -xvidencopts bitrate=800:vhq=4:turbo:pass=2 -nosound
Play the video:
$ mplayer divx.avi
...
VIDEO: [XVID] 720x576 12bpp 25.000 fps 644.3 kbps (78.7 kbyte/s)
...
To encode the audio in Ogg Vorbis:
$ oggenc -q 3 audio.wav
To pack everything in a Matroska container:
$ mkvmerge -o video.mkv video.avi audio.ogg video.srt
Note that an MKV can contain several video streams and several audio tracks in different codecs and several subtitles.
To extract the first video and the second audio from an MKV:
$ mkvextract tracks video.mkv 1:video.avi 3:audio.ogg
To assemble a video encoded in several parts into one file:
$ mencoder -forceidx -ovc copy -oac copy -o video.avi video_1.avi video_2.avi ...
To extract the video from an AVI:
$ mplayer -dumpvideo movie.avi -dumpfile video.avi
To extract the audio from an AVI:
$ mplayer -dumpaudio movie.avi -dumpfile audio.mp3
To assemble a video and an audio in an AVI:
$ ffmpeg -i video.avi -i audio.ac3 -vcodec copy -acodec copy movie.avi
To encode a VOB in MP4 with the video in H.264 and the audio in AAC while removing 8 pixels black stripes on the left and the right then resizing the video from 720x480 pixels to 640x360 pixels in 16:9:
$ ffmpeg -i video.vob -acodec libfdk_aac -vcodec libx264 -preset slow -vf crop=704:480:8:0,scale=640:360,setsar=1/1,setdar=16/9 -b:v 1600k -aspect 16/9 -f mp4 -y video.mp4
To let the encoder compute the best compression rate for a given quality (Constant Rate Factor):
ffmpeg -i video.vob -acodec libfdk_aac -vcodec libx264 -preset veryslow -crf 19 -f mp4 -y video.mp4
To obtain 720x404 pixels in 2 passes:
$ ffmpeg -i video.vob -pass 1 -an -vcodec libx264 -preset slow -vf crop=704:480:8:0,scale=720x404,setsar=1/1,setdar=16/9 -b:v 1600k -aspect 16/9 -f mp4 -y /dev/null
$ ffmpeg -i video.vob -pass 2 -acodec libfdk_aac -vcodec libx264 -preset slow -vf crop=704:480:8:0,scale=720x404,setsar=1/1,setdar=16/9 -b:v 1600k -aspect 16/9 -f mp4 -y video.mp4
If ffmpeg
us suggesting to use use the mpeg4_unpack_bframes
bitstream filter:
$ ffmpeg -i video.avi -acodec copy -vcodec copy -bsf:v mpeg4_unpack_bframes video2.avi
Extracting a CD
We hear frequencies from 20 Hz to 20 kHz - from 10 Hz to 25 kHz at best and not more than 18 kHz as we age. The theoretical limit of the encoder in the high spectrum in FLAC and in MP3 depending on the bit rate:
FLAC | MP3 320 | MP3 256 | MP3 192 | MP3 128 |
22 kHz | 20,5 kHz | 20 kHz | 19 kHz | 16 kHz |
A WAV seen by the acoustic spectrum analyser Spek:
This WAV encoded in FLAC:
The same encoded in MP3 320 kbps:
And now in MP3 192 kbps:
And again in AAC 320 kbps:
And finally in OGG 320 kbps:
The MP3 320, the AAC 320 and the OGG 320 are 2 times smaller than the FLAC which is 2 times smaller than the WAV.
To locate a particular title on a CD:
$ mplayer cdda://1
NOTE: If the CD was mounted by the system, you can simply copy the tracks on the disk.
To extract a particular title in WAV:
$ mplayer cdda://1 -ao pcm:file=track1.wav
To encode the audio track in MP3:
$ lame -V 6 track1.wav track1.mp3
This command encodes the audio in VBR (Variable BitRate) with a quality level of 6. Use a lower value for better quality if needed.
To listen to the audio track:
$ mplayer track1.mp3
Opening audio decoder: [mp3lib] MPEG layer-2, layer-3
AUDIO: 44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)
Selected audio codec: [mp3] afm: mp3lib (mp3lib MPEG layer-2, layer-3)
lame
has some built-in options:
$ lame -v --preset fast standard track1.wav track1.mp3
This version enables the new fast VBR encoder and sets the quality level to standard:
$ mplayer track1.mp3
Opening audio decoder: [mp3lib] MPEG layer-2, layer-3
AUDIO: 44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)
Selected audio codec: [mp3] afm: mp3lib (mp3lib MPEG layer-2, layer-3)
Try different quality settings and settle for the smallest file which sounds good enough.
To encode a WAV in Ogg Vorbis, use oggenc
:
$ oggenc -q 3 track1.wav
Listen to it:
$ mplayer track1.ogg
Opening audio decoder: [ffmpeg] FFmpeg/libavcodec audio decoders
AUDIO: 44100 Hz, 2 ch, s16le, 112.0 kbit/7.94% (ratio: 14000->176400)
Selected audio codec: [ffvorbis] afm: ffmpeg (FFmpeg Vorbis)
To encode a OGG in MP3, go through a WAV:
$ oggdec audio.ogg
$ lame -q2 audio.wav audio.mp3
$ rm audio.wav
To encode a whole directory:
$ oggdec *.ogg
$ for f in *.wav; do lame -h --vbr-new "$f" "$(basename "$f" .wav).mp3"; done
$ rm *.wav
NOTE: Don't forget to protect file names which could contain spaces with double quotes.
To convert a WMA in a WAV, use ffmpeg:
$ ffmpeg -i audio.wma audio.wav
To encode a WAV in FLAC:
$ ffmpeg -i audio.wav -af aformat=s16:44100 audio.flac
Encodes the track to 16-bit and 44.1 kHz.
To directly convert a WMA in a MP3:
$ ffmpeg -i audio.wma -acodec libmp3lame -ar 22050 -ab 64k audio.mp3
This command sets the audio sampling frequency to 22050 Hz and the audio bitrate to 64k. Check the result:
$ mplayer audio.mp3
Opening audio decoder: [mp3lib] MPEG layer-2, layer-3
AUDIO: 22050 Hz, 2 ch, s16le, 64.0 kbit/9.07% (ratio: 8000->88200)
Selected audio codec: [mp3] afm: mp3lib (mp3lib MPEG layer-2, layer-3)
To encode in MP3 a whole directory in FLAC:
$ for f in *.flac; do ffmpeg -i "$f" -acodec libmp3lame -ar 44100 -ab 192k "$(basename "$f" .flac).mp3"; done
NOTE: For a better quality, replace the parameters -ar 44100 -ab 192k
by -ar 48000 -ab 320k
.
To encore a directory in WMA, replace the extension .flac by .wma.
Add the option -map a
to only copy the audio stream without any other contents like images.
To add an album cover contained in Folder.jpg to an entire MP3 directory :
$ for f in *.mp3; do ffmpeg -i "$f" -i Folder.jpg -map 0:0 -map 1:0 -c copy -id3v2_version 3 audio.mp3 && mv -f audio.mp3 "$f"; done
For a FLAC directory:
$ metaflac --import-picture-from=Folder.jpg *.flac
To extract all the audio tracks in FLAC from a file APE with a CUE directory:
$ sudo add-apt-repository -y ppa:flacon
$ sudo apt-get update
$ sudo apt-get install -y flacon
$ shntool split -f audio.cue -o flac -t '%n - %t' audio.ape
Replace audio.ape by audio.wav or audio.flac if the file is in WAV or in FLAC.
If the audio is encoded in FLAC 24 bits, convert it first in FLAC 16 bits:
$ ffmpeg -i audio24.flac -af aformat=s16:44100 audio16.flac
For a audio.m4a file, start by converting the ALAC in WAV:
$ alac-decoder -f audio.wav audio.m4a
To encode a whole directory extracted from a CD in FLAC:
$ for f in *.wav; do ffmpeg -i "$f" -af aformat=s16:44100 "$(basename "$f" .wav).flac"; done
If all the files are in the APE format:
$ for f in *.ape; do ffmpeg -i "$f" -af aformat=s16:44100 "$(basename "$f" .ape).flac"; done
If the WAV is in ia WV:
$ wvunpack audio.wv
To list all the tags in a FLAC:
$ metaflac --export-tags-to=- audio.flac
To remove a tag from all the tracks in a folder:
$ metaflac --remove-tag=ARTISTSORT *.flac
To set the track number of all the tracks in a folder:
$ n=1; for f in *.flac; do metaflac --remove-tag=TRACKNUMBER --set-tag=TRACKNUMBER=`printf '%02d' $n` "$f"; n=$((n+1)); done
To convert a whole set of FLAC files in MP3 while truncating them to 30 seconds with a fade effect of 2 seconds:
$ for f in *.flac; do ffmpeg -i "$f" -vn -acodec libmp3lame -ab 192k -t 30 -filter_complex "[0:a]afade=t=out:d=2:st=28" "$(basename "$f" .flac).mp3"; done
To edit the information about an audio track or several tracks in one shot, use EasyTag.
Converting audio and video files
To watch an episode of your favorite series on your Android, first get the properties of the AVI:
$ mplayer video.avi
VIDEO: [XVID] 624x352 24bpp 23.976 fps 1000.0 kbps (122.1 kbyte/s)
AUDIO: 48000 Hz, 2 ch, s16le, 128.0 kbit/8.33% (ratio: 16000->192000)
Let's say the video is 624x352 with 24 frames per second and a bitrate of 1000k while the audio has a frequency of 48000 Hz and a bitrate of 128k. The bitrates of the audio and the video should be reduced so the resulting file isn't too big but the quality is still acceptable. An audio bitrate of 64k with a frequency of 44100 Hz and a video bitrate of 600k are a good compromise.
The aspect ratio of the video is 624/352=1.77. The screen of the phone is 480x320. We compute the new height of the video by dividing the width of the screen by the aspect ratio of the video, that is 480/1.77=271, which we round up to 272 so the dimension is a multiple of 16. The new width of the video will be the width of the screen.
If the computed height is bigger than the height of the screen, multiply the height of the screen by the aspect ratio of the video to compute the new width of the video. Its height will be the height of the screen. For example, to fit a video which is 720x576 in a 480x320 screen, dividing 480 by 1.25 (720/576) gives us a height of 384 which is too large so instead we multiply 320 by 1.25 to obtain a width of 400. The resized video will be 400x320.
To fit a video for a particular display, recompute its width and its height with the following formular:
aspect_ratio_of_display = width_of_display / height_of_display
aspect_ratio_of_video = width_of_video / height_of_video
if aspect_ratio_of_video > aspect_ratio_of_display
then height = (height_of_video / aspect_ratio_of_video) and width = width_of_display
else width = (height_of_display x aspect_ratio_of_video) and height = height_of_display
Remember to round the width and the height of the video to a multiple of 16.
If playing a video at a frame rate of 24 or 25 fps isn't supported by the video player of the phone, or to reduce the size of the file even more, change it to a lower 15 fps.
Convert the video file with ffmpeg
in 3GP/AAC/H.264 with the correct frame rate and aspect ratio, the new size, and lower bitrates for the sound and the video:
$ ffmpeg -i video.avi -acodec libfdk_aac -ar 44100 -ab 64k \
-vcodec libx264 -aspect 1.77 -s 480x272 -r 24 -b 600k video.3gp
- video.3gp
- The resulting file in a 3GP container.
- -f 3gp
- Add this optional parameter to specify the format of the file.
- -i video.avi
- The input file.
- -acodec libfdk_aac
- The audio is encoded in AAC.
- -ar 44100
- The audio frequency is 44100 Hz.
- -ab 64k
- The audio bitrate is 64k.
- -vcodec libx264
- The video is encoded in H.264.
- -vpre default
- Default settings for the video encoder.
- -aspect 1.77
- The aspect ratio of the video is 1.77.
- -s 480x272
- The video is 480 by 272 pixels.
- -r 24
- The video frame rate is 24 fps.
- -b 600k
- The video bitrate is 600k.
IMPORTANT: ffmpeg can vary from one version to another. If the one you have installed isn't compiled with libx264, replace -vcodec libx264
with -vcodec h264
.
If ffmpeg
asks you to select an encoding preset, add the parameter -vpre default
after -vcodec libx264
. The option files are in the folder /usr/share/ffmpeg.
To crop the video, add -vf crop=w:h:x:y
.
To stream the video, add -movflags faststart
so metadata for the audio and the video are at the beginning of the container.
To adjust the audio volume, add -af volume=n
where n
is a positive or negative value in decibels (+3 doubles the volume).
To encode the audio in MP3, replace -acodec libfdk_aac
with -acodec libmp3lame
.
If the video and the audio are not synchronized, add the option -itsoffset
with in argument the number of seconds of delay between the video and the audio.
If the program complains about B-Frames, try to add -fflags +genpts
before the -i
parameter.
To encode a video in 2 passes, run a first pass without generating the audio while throwing away the video output, then run the complete command in a second pass:
$ ffmpeg -i video.avi -pass 1 -an \
-vcodec libx264 -aspect 1.77 -s 480x272 -r 24 -b 600k -f mp4 -y /dev/null
$ ffmpeg -i video.avi -pass 2 -acodec libfdk_aac -ar 44100 -ab 64k \
-vcodec libx264 -aspect 1.77 -s 480x272 -r 24 -b 600k video.mp4
The same video for an iPhone with some variations:
$ ffmpeg -i video.avi -acodec libfdk_aac -ab 128k -vcodec mpeg4 -g 300 \
-r 25 -aspect 1.77 -b 600k -s 480x272 video.mp4
NOTE: This format works as well for an Android.
If you have a PSP:
$ ffmpeg -i video.avi -acodec libfdk_aac -ab 32k -ar 24000 -vcodec libx264 -aspect 1.77 -b:v 300k -s 320x180 video.mp4
Another variation for an iPad which encodes the video stream in H.264 and the audio stream in AAC separately:
$ ffmpeg -threads 4 -g 30 -partitions +parti4x4+parti8x8+partp8x8 -i_qfactor 0.71 -qcomp 0.6 \
-i video.avi -aspect 1.77 -y \
-f h264 -vcodec libx264 -qmax 27 -r 24 video.h264 \
-f adts -ar 48000 -f wav -ac 2 - | faac -b 128 -o audio.aac -
Notice that the audio stream is extracted in WAV and piped into the faac
command.
Since the screen of an iPad is large enough, the video isn't resized.
Assemble the 2 files video.h264 and audio.aac in a MP4 container with the MP4Box
utility program:
$ MP4Box -fps 24 -add video.h264 -add audio.aac video.mp4
Adjust the parameter -fps
according to the frame rate of the video to 24, 25 or even 23.98.
List the contents of the MP4:
$ ffprobe video.mp4
To convert a WMV or a FLV while preserving the video and audio parameters, start by displaying the properties of the 2 streams:
$ ffprobe video.wmv
Stream #0:0(fre): Audio: wmav2 (a[1][0][0] / 0x0161), 48000 Hz, 2 channels, s16, 64 kb/s
Stream #0:1(fre): Video: wmv3 (Main) (WMV3 / 0x33564D57), yuv420p, 640x360, 650 kb/s, 25 tbr, 1k tbn, 1k tbc
Encode the 2 streams separately:
$ ffmpeg -i video.wmv -aspect 1.77 -y \
-f h264 -vcodec libx264 -qmax 27 -r 25 -b:v 600k -s 640x360 video.h264 \
-f adts -ar 48000 -f wav -ac 2 - | faac -b 64 -o audio.aac -
Assemble the 2 files:
$ MP4Box -fps 25 -add video.h264 -add audio.aac video.mp4
To extract a sound stream in AAC and insert it back while forcing the conversion in the MPEG-4 format:
$ MP4Box -raw 2 video.mp4 -out audio.aac
$ MP4Box -rem 2 video.mp4
$ MP4Box -add audio.aac video.mp4
Check the result with ffplay
:
$ ffplay video.mp4
You may also use mencoder
if you have a version compiled with MP4 and AAC support:
$ mencoder -of lavf -lavfopts format=mp4 -sws 9 -af volnorm -srate 44100 -channels 2 -vf-add harddup \
-oac faac -faacopts br=96:mpeg=4:object=2:raw -ovc x264 -x264encopts \
bitrate=800:threads=auto:bframes=0:frameref=2:global_header:partitions=all \
-o video.mp4 video.avi
Wrap this command in a script called mp4avi
:
#
faacopts="br=96:mpeg=4:object=2:raw"
x264encopts="bitrate=1000:threads=auto:bframes=0:frameref=2:global_header:partitions=all"
abort () {
echo $1 >&2
exit ${2-1}
}
for f in $*
do
ext=`echo -n $f | sed 's/.*\.//'`
if [ ! -f $f -o "$ext" != "avi" ]
then
abort "$f?"
fi
file=`basename $f .avi`
mencoder -of lavf -lavfopts format=mp4 -sws 9 -af volnorm -srate 44100 -channels 2 -vf-add harddup \
-oac faac -faacopts $faacopts \
-ovc x264 -x264encopts $x264encopts \
$f -o $file.mp4
if [ $? -ne 0 ]
then
abort "$f?"
fi
done
Copy the script in your bin directory:
$ cp mp4avi ~/bin; chmod 755 ~/bin/mp4avi
Run it in the directory where you have saved the AVI files:
$ mp4avi *.avi
Adjust the parameter bitrate
according to the bitrate of the video stream in input and to the desired quality of the output.
To convert a MP4 in AVI while resizing the video to 640x360 pixels:
$ ffmpeg -i video.mp4 -vcodec libxvid -s:v 640x360 -b:v 1200k -q:v 3 -acodec ac3 -ar 44100 -ab 192k -ac 2 video.avi
Adjust the parameters to the input format and to the desired result.
To convert a MKV in MP4:
#
abort () {
echo $1 >&2
exit ${2-1}
}
trap "rm -f video.h264 audio.aac" 0 1 2 3 15
for f in $*
do
ext=`echo -n $f | sed 's/.*\.//'`
if [ ! -f $f -o "$ext" != "mkv" ]
then
abort "$f?"
fi
file=`basename $f .mkv`
mkvextract tracks $f 1:video.h264 2:audio.aac
MP4Box -add video.h264 -add audio.aac $file.mp4
if [ $? -ne 0 ]
then
abort "$f?"
fi
done
$ cp mp4mkv ~/bin; chmod 755 ~/bin/mp4mkv
$ mp4mkv *.mkv
If the program complains about a missing stream, edit the script and change the track numbers:
$ mkvextract tracks $f 0:video.h264 1:audio.aac
Add the parameter -fps
to MP4Box to force the frame rate.
If the aspect ratio is wrong, correct the PAR with the formula PAR = DAR รท SAR where DAR is usually 1.78 (16:9) or 1.33 (4:3) but values like 1.85:1 and 2.39:1 are also common:
$ MP4Box -fps 23.98 -par 1=4:3 -add video.h264 -add audio.aac video.mp4
To convert an AC3 audio in AAC:
$ ffmpeg -i audio.ac3 -acodec libfdk_aac audio.aac
To convert a DTS audio in AAC:
$ ffmpeg -i audio.dts -f adts -ar 48000 -f wav -ac 2 - | faac -b 96 -o audio.aac -
To extract subtiles encoded in ASS and convert them in SRT:
$ mkvextract tracks video.mkv 2:subtitles.ass
$ ffmpeg -i subtitles.ass subtitles.srt
If the MKV contains a video encoded in H265, a format which is not supported yet by your TV, transcode the video in H264:
#
USAGE="`basename $0` [-h] [-a] file.mkv..."
abort () {
echo $1 >&2
exit ${2-1}
}
trap "rm -f video.h264 video.h265 audio.aac audio.ac3" 0 1 2 3 15
ac3=0
# parse command line
set -- `getopt ah $*`
if [ $? != 0 ]
then
abort "$USAGE" 1
fi
while [ $1 != -- ]
do
case $1 in
-h)
abort "$USAGE" 0
;;
-a)
ac3=1
;;
esac
shift # next flag
done
shift # skip --
if [ $# = 0 ]
then
abort "$USAGE" 1
fi
for f in $*
do
ext=`echo -n $f | sed 's/.*\.//'`
if [ ! -f $f -o "$ext" != "mkv" ]
then
abort "$f?"
fi
file=`basename $f .mkv`
if [ $ac3 ]
then
mkvextract tracks $f 0:video.h265 1:audio.ac3
ffmpeg -i audio.ac3 -acodec libfdk_aac audio.aac
else
mkvextract tracks $f 0:video.h265 1:audio.aac
fi
ffmpeg -i video.h265 -c:v libx264 -preset medium video.h264
MP4Box -add video.h264 -add audio.aac $file.mp4
if [ $? -ne 0 ]
then
abort "$f?"
fi
done
If the audio in the MKV is in AC3, add the option -a
to convert it in AAC.
To build a MKV, replace the command MP4Box
with mkvmerge
:
$ mkvmerge -o video.mkv video.h264 audio.aac
If the video in the MKV is encoded with an aspect ratio and a display ratio which your TV doesn't interpret properly, resize the video with a SAR 1x1 while keeping the other streams:
$ ffmpeg -i video.mkv -acodec copy -scodec copy -vcodec libx264 -b:v 1000k -vf scale=720:404,setsar=1/1,setdar=16/9 -aspect 16/9 -y video2.mkv
Adapt the parameter -b:v 1000k
to the source.
To add subtitles to a MKV:
$ ffmpeg -i video.mkv -i video.srt -vcodec copy -acodec copy -scodec copy video2.mkv
For a whole folder:
$ for f in *.mkv; do ffmpeg -i "$f" -i "$(basename "$f" .mkv)".srt -map_metadata -1 -vcodec copy -acodec copy -scodec copy video.mkv && mv video.mkv "$f"; done
To remove subtitles from a MKV:
$ mkvmerge -S video.mkv -o video2.mkv
To add a SRT:
$ mkvmerge video.mkv video.srt -o video2.mkv
For a MP4:
$ ffmpeg -i video.mp4 -i video.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:1 language=eng -metadata:s:2 language=eng video2.mp4
More simply with MP4Box:
$ MP4Box -add video.srt video.mp4
To extract a subtitle in SRT from a MP4:
$ ffmpeg -i video.mp4 -vn -an -codec:s srt video.srt
To convert a MP4 in MKV:
#
abort () {
echo $1 >&2
exit ${2-1}
}
trap "rm -f video.h264 audio.aac" 0 1 2 3 15
for f in $*
do
ext=`echo -n $f | sed 's/.*\.//'`
if [ ! -f $f -o "$ext" != "mp4" ]
then
abort "$f?"
fi
file=`basename $f .mp4`
MP4Box -raw 1:output=video.h264 $f
MP4Box -raw 2:output=audio.aac $f
if [ -f $file.srt ]
then
mkvmerge --disable-track-statistics-tags -o $file.mkv video.h264 --aac-is-sbr 0 audio.aac $file.srt
else
mkvmerge --disable-track-statistics-tags -o $file.mkv video.h264 --aac-is-sbr 0 audio.aac
fi
if [ $? -ne 0 ]
then
abort "$f?"
fi
done
NOTE: mkvmp4
will automatically merge a SRT with the same name than the MP4.
mkvmerge
is able to directly manage all input formats:
$ mkvmerge --disable-track-statistics-tags -o video.mkv video.mp4 video.srt
To build a MP4 from a WEBM containing a video in VP8 and an audio in OGG, copy the following script in your bin directory:
#
abort () {
echo $1 >&2
exit ${2-1}
}
trap "rm -f video.vp8 audio.ogg audio.wav video.h264 audio.aac" 0 1 2 3 15
for f in $*
do
ext=`echo -n $f | sed 's/.*\.//'`
if [ ! -f $f -o "$ext" != "webm" ]
then
abort "$f?"
fi
file=`basename $f .webm`
fps=`ffprobe $f 2>&1 | grep "Stream #0:0" | sed "s/.*, \(.*\) fps.*/\1/"`;
mkvextract tracks $f 1:video.vp8 2:audio.ogg
ffmpeg -i video.vp8 -vcodec libx264 -r $fps video.h264
oggdec audio.ogg
faac -b 96 -o audio.aac audio.wav
MP4Box -fps $fps -add video.h264 -add audio.aac $file.mp4
if [ $? -ne 0 ]
then
abort "$f?"
fi
done
If the WEBM contains a video encoded in VP9 or an audio in OPUS, replace video.vp8
by video.vp9
and audio.ogg
by audio.opus
in the script, and to decode the audio in WAV, use the command opusdec audio.opus
.
To convert a FLV already containing a video in H264 and an audio in AAC in MP4, such as a TV program downloaded in replay, copy the following script in your bin directory:
#
abort () {
echo $1 >&2
exit ${2-1}
}
trap "rm -f video.mp4 video.h264 audio.aac" 0 1 2 3 15
for f in $*
do
ext=`echo -n $f | sed 's/.*\.//'`
if [ ! -f $f -o "$ext" != "flv" ]
then
abort "$f?"
fi
file=`basename $f .flv`
ffmpeg -i $f -acodec copy -vcodec copy -y video.mp4
MP4Box -raw 1 video.mp4 -out video.h264
MP4Box -raw 2 video.mp4 -out audio.aac
MP4Box -add video.h264 -add audio.aac $file.mp4
done
$ cp mp4flv ~/bin; chmod 755 ~/bin/mp4flv
$ mp4flv *.flv
If the FLV contains an audio in MP3, extract the video while converting the MP3 in AAC:
$ ffmpeg -i video.flv -vcodec copy -y video.h264 -f adts -ar 44100 -f wav -ac 2 - | faac -b 128 -o audio.aac -
To adjust the audio volume, add -af volume=n
where n
is a positive or negative value in decibels (+3 doubles the volume).
Assemble the MP4 with MP4Box
:
$ MP4Box -add video.h264 -add audio.aac video.mp4
To repack a TS containing also a video in H264 and an audio in AAC in a MP4, you can extract the streams with ffmpeg
and build the MP4 with MP4Box
.
Start by displaying the frame rate and the bit rate of the video with ffprobe
.
Pass the bit rate to ffmpeg
with the parameter -bt
and the frame rate to MP4Box
with the parameter -fps
:
$ ffprobe video.ts
...
Input #0, mpegts, from 'video.ts':
Duration: 01:39:47.98, start: 0.100511, bitrate: 1678 kb/s
Stream #0:0[0x100]: Video: h264 (Main), yuv420p, 852x480 [SAR 1:1 DAR 71:40], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:1[0x101]: Audio: aac, 44100 Hz, stereo, fltp, 130 kb/s
...
$ ffmpeg -i video.ts -an -vcodec copy -bt 1200k video.h264
$ ffmpeg -i video.ts -acodec copy -vn audio.aac
$ MP4Box -fps 29.97 -add video.h264 -add audio.aac video.mp4
Here is a script:
#
USAGE="`basename $0` [-h] -f fps -b bitrate file.ts..."
abort () {
echo $1 >&2
exit ${2-1}
}
trap "rm -f video.h264 audio.aac" 0 1 2 3 15
fps=
bt=
# parse command line
set -- `getopt b:f:h $*`
if [ $? != 0 ]
then
abort "$USAGE" 1
fi
while [ $1 != -- ]
do
case $1 in
-h)
abort "$USAGE" 0
;;
-f)
fps="$2"
shift
;;
-b)
bt="$2"
shift
;;
esac
shift # next flag
done
if [ -z "$fps" -o -z "$bt" ]
then
abort "$USAGE" 1
fi
shift # skip --
if [ $# = 0 ]
then
abort "$USAGE" 1
fi
for f in $*
do
ext=`echo -n $f | sed 's/.*\.//'`
if [ ! -f $f -o "$ext" != "ts" ]
then
abort "$f?"
fi
file=`basename $f .ts`
ffmpeg -i $f -an -vcodec copy -bt $bt video.h264
ffmpeg -i $f -acodec copy -vn audio.aac
MP4Box -add video.h264 -add audio.aac $file.mp4
if [ $? -ne 0 ]
then
abort "$f?"
fi
done
To share your movies recorded with a camcorder on YouTube, convert the videos in Flash Video:
$ ffmpeg -i video.mpg -acodec libmp3lame -ar 22050 -ab 32K -vcodec flv -aspect 1.33 -s 320x240 -r 25 -b 600k video.flv
- video.flv
- The resulting file in a Flash Video container.
- -f flv
- Add this optional parameter to specify the format of the file.
- -i video.mpg
- The input file.
- -acodec libmp3lame
- The audio is encoded in MP3.
- -ar 22050
- The audio frequency is 22050 Hz.
- -ab 32k
- The audio bitrate is 32k.
- -vcodec flv
- The video is encoded in Flash Video.
- -aspect 1.33
- The aspect ratio of the video is 1.33.
- -s 320x240
- The video is 320 by 240 pixels.
- -r 25
- The video frame rate is 25 fps.
- -b 600k
- The video bitrate is 600k.
To take a picture of a video, let's say about 5 minutes after the beginning, generate a series of snapshots with the proper size after a delay of 300 seconds:
$ ffmpeg -i video.flv -ss 300 -s 320x240 -r 1 snap-%03d.jpg
Press q to stop the program after a short while and choose one of the snap-*.jpg images.
To extract an audio in WAV :
$ ffmpeg -i video.flv -f wav audio.wav
To encode an audio in AAC in a M4A container:
$ faac -w -b 128 -o audio.m4a audio.wav
To extract an audio and encode it in MP3:
$ ffmpeg -i video.flv -acodec libmp3lame -ar 44100 -ab 64K audio.mp3
To extract an audio already encoded in MP3:
$ ffmpeg -i video.flv -acodec copy audio.mp3
To extract a video already encoded in MP4:
$ ffmpeg -i video.flv -an -vcodec copy video.mp4
To mix a video and an audio:
$ ffmpeg -i audio.mp3 -acodec copy -i video.mp4 -vcodec copy video.flv
To truncate a video, cut a clip:
$ ffmpeg -i video.mp4 -ss 00:00:45 -t 30 -vcodec copy -acodec copy clip.mp4
To cut the video at the nearest keyframe, specify the option -ss
before the option -i
:
$ ffmpeg -ss 00:00:45 -t 30 -i video.mp4 -vcodec copy -acodec copy clip.mp4
To remove the title of a video:
$ ffmpeg -i video.mp4 -metadata title='' -metadata comment='' -acodec copy -vcodec copy video2.mp4
To remove the metadata:
$ ffmpeg -i video.mkv -map_metadata -1 -c copy -map 0 video2.mkv
To convert a media for HTML5:
$ ffmpeg -i video.flv -vn -f wav audio.wav
$ ffmpeg -i video.flv -an -vcodec copy video.mp4
$ oggenc -q3 audio.wav
$ ffmpeg -i audio.ogg -acodec copy -i video.mp4 -vcodec libvpx -aspect 1.77 -s 480x272 -r 24 -b:v 300k -quality good -cpu-used 0 -qmin 10 -qmax 42 video.webm
To return a WEBM to a web client, add the following line in the file /etc/mime.types and restart Apache:
video/webm webm
If you can't modify system files, add the following line to a .htaccess file in the directory containing the video:
AddType video/webm webm
Read the article Broadcast a video from a website to learn how to insert a media in a website.
Comments