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.
Installation

Linux
Medibuntu is a repository of packages that cannot be included into the Ubuntu distribution for legal reasons (copyright, license, patent, etc). 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.
Add Medibuntu's repository to your system and add to your keyring the GPG key which is needed to authenticate the packages:
$ sudo wget -O /etc/apt/sources.list.d/medibuntu.list http://www.medibuntu.org/sources.list.d/$(lsb_release -cs).list
$ sudo apt-get -q update
$ sudo apt-get -q -y --allow-unauthenticated install medibuntu-keyring app-install-data-medibuntu
$ sudo apt-get -q update
NOTE: Remember to run these commands again after a release upgrade.
Install the codecs:
$ sudo apt-get install w32codecs
or
$ sudo apt-get install w64codecs
To play DVDs, the libdvdcss2 package is essential:
$ sudo apt-get install libdvdcss2
Install mencoder with mplayer:
$ sudo apt-get install mplayer mencoder
Add tools to create and manipulate MP3 or Vorbis audio files, OGM media streams and Matroska and MP4 containers:
$ sudo apt-get install lame vorbis-tools ogmtools mkvtoolnix mkvtoolnix-gui mpeg4ip-server
Install ffmpeg with the libraries from Medibuntu which are compiled with support for encoding the audio in AAC and the video in H.264:
$ sudo apt-get install ffmpeg ffprobe libavcodec-extra-52 libavutil-extra-49
Check if an audio can be encoded in AAC:
$ ffmpeg -formats | grep aac
D aac raw ADTS AAC
D A aac Advanced Audio Coding
EA libfaac libfaac AAC (Advanced Audio Codec)
The last line with an E tells us that ffmpeg is effectively compiled with libfaac which supports encoding a sound track in AAC.
NOTE: If the command doesn't list libfaac, try replacing -formats by -codecs.
Also check if a video can be encoded in H.264:
$ ffmpeg -formats | grep 264
DE h264 raw H.264 video format
E ipod iPod H.264 MP4 format
D V D h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
D V D h264_vdpau H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)
EV libx264 libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
We'll use libx264 to encode a video stream in H.264.

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-get install libsdl-dev
To build the package, download the source code here and type the following commands:
$ cd SDL-1.2.15
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
NOTE: Before you run configure, install the ESD library and libxv for developers with sudo apt-get install libxv-dev libesd0-dev.
MP3
To install the version of the system:
$ sudo apt-get install libmp3lame-dev
To build the package, download lame and type the following commands:
$ cd lame-3.98.4
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
AAC
To install the version of the system:
$ sudo apt-get install libfaac-dev
To build the package, download faac and type the following commands:
$ cd faac-1.28
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
Ogg Vorbis
To install the version of the system:
$ sudo apt-get install libogg-dev libvorbis-dev
To build the package, download libogg and libvorbis, then type the following commands:
$ cd libogg
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
$ cd libvorbis
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
H.264
Make sure the assembler Yasm is installed:
$ sudo apt-get install yasm
To download and build the package, type the following commands:
$ git clone git://git.videolan.org/x264.git x264
$ cd x264
$ ./configure --prefix=/usr/local --enable-static
$ 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.
VP8
To download and build the package, type the following commands:
$ git clone http://git.chromium.org/webm/libvpx.git libvpx
$ cd libvpx
$ ./configure --prefix=/usr/local
$ make
$ sudo make install
Xvid
Pour installer la version du système :
$ sudo apt-get 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
Download libebml and libmatroska then type the following commands:
$ cd libebml-1.2.2/make/linux
$ make staticlib
$ sudo make install_staticlib install_headers
$ cd libmatroska-1.3.0/make/linux
$ make staticlib
$ sudo make install_staticlib install_headers
FFmpeg
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-libfaac --enable-libmp3lame --enable-libvorbis \
--enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab \
--enable-ffplay
$ make clean
$ make all
$ sudo make install
Installs ffmpeg, ffplay, ffprobe and ffserver.
MPlayer
Download the source code:
$ svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer
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-get install zlib1g-dev.
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 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 -ifo video.ifo -vobsubout subtitles -vobsuboutindex 0 -sid 0 -o /dev/null -nosound -ovc frameno
$ mencoder video.vob -ifo video.ifo -vobsubout subtitles -vobsuboutindex 1 -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 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 -ovc x264 -x264encopts \
bitrate=800:frameref=8:mixed_refs:bframes=3:subq=3:weight_b:pass=1
$ mencoder video.vob -o video.avi -nosound -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: Certain 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.
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 ...

Extracting a CD
To locate a particular title on a CD:
$ mplayer cdda://1
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, pass by 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 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 encore in MP3 a whole directory in FLAC:
$ for f in *.flac; do ffmpeg -i "$f" -acodec libmp3lame -ar 44100 -ab 128k "$(basename "$f" .flac).mp3"; done

Converting a video for a mobile equipment
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 libfaac -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 libfaac
- 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 -acodec libfaac doesn't work, try -acodec aac. 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 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 libfaac 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.
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 -pass 1 -i video.avi -an \
-vcodec libx264 -aspect 1.77 -s 480x272 -r 24 -b 600k -f mp4 -y /dev/null
$ ffmpeg -pass 2 -i video.avi -acodec libfaac -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 libfaac -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 libfaac -ab 32 -ar 24000 -vcodec xvid -aspect 1.77 -b 300k -s 320x176 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 mp4creator utility program:
$ mp4creator -r 24 video.h264 video.m4v
$ mp4creator -aac-profile=4 audio.aac video.m4v
Adjust the parameter -r according to the frame rate of the video to 24, 25 or even 23.98.
List the contents of the MP4:
$ mp4creator -l video.m4v
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 -
To extract a sound stream in AAC and insert it back while forcing the conversion in the MPEG-4 format:
$ mp4creator -e 2 video.m4v
$ mv video.m4v.t2 audio.aac
$ mp4creator -d 2 video.m4v
$ mp4creator -aac-profile=4 audio.aac video.m4v
Check the result with ffplay:
$ ffplay video.m4v
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.m4v video.avi
Wrap this command in a script called m4vavi:
#
faacopts="br=96:mpeg=4:object=2:raw"
x264encopts="bitrate=800: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.m4v
if [ $? -ne 0 ]
then
abort "$f?"
fi
done
Copy the script in your bin directory:
$ cp m4vavi ~/bin; chmod 755 ~/bin/m4vavi
Run it in the directory where you have saved the AVI files:
$ m4vavi *.avi
Adjust the parameter bitrate according to the bitrate of the video stream in input and to the desired quality of the output.
To build a M4V from a MP4 already containing a video in H264 and an audio in AAC, copy the following script in your bin directory:
#
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`
h264=`mp4creator -list $f | grep video | cut -f1`
fps=`mp4creator -list $f | grep video | cut -f3 | sed 's/.*@ \(.*\) fps.*/\1/'`
aac=`mp4creator -list $f | grep audio | cut -f1`
mp4creator -extract=$h264 $f video.h264
mp4creator -extract=$aac $f audio.aac
mp4creator -r $fps video.h264 $file.m4v
mp4creator -aac-profile=4 audio.aac $file.m4v
if [ $? -ne 0 ]
then
abort "$f?"
fi
done
$ cp m4vmp4 ~/bin; chmod 755 ~/bin/m4vmp4
$ m4vmp4 *.mp4
A variation for a 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" != "mkv" ]
then
abort "$f?"
fi
file=`basename $f .mkv`
mkvextract tracks $f 1:video.h264 2:audio.aac
mp4creator -r 25 video.h264 $file.m4v
mp4creator -aac-profile=4 audio.aac $file.m4v
if [ $? -ne 0 ]
then
abort "$f?"
fi
done
$ cp m4vmkv ~/bin; chmod 755 ~/bin/m4vmkv
$ m4vmkv *.mkv
To convert a FLV already containing a video in H264 and an audio in AAC in M4V, 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
mp4creator -extract=1 video.mp4 video.h264
mp4creator -extract=2 video.mp4 audio.aac
mp4creator -r 25 video.h264 $file.m4v
mp4creator -aac-profile=4 audio.aac $file.m4v
done
$ cp m4vflv ~/bin; chmod 755 ~/bin/m4vflv
$ m4vflv *.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 M4V with mp4creator:
$ mp4creator -r 25 video.h264 video.m4v
$ mp4creator -aac-profile=4 audio.aac video.m4v
To repack a TS containing also a video in H264 and an audio in AAC in a M4V, you can extract the streams with ffmpeg and build the M4V with mp4creator.
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 mp4creator with the parameter -r:
$ 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
$ mp4creator -r 23.97 video.h264 video.m4v
$ mp4creator -aac-profile=4 audio.aac video.m4v

Converting a video for the web
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 cut a clip of:
$ ffmpeg -i video.mp4 -ss 00:00:45 -t 30 -vcodec copy -acodec copy clip.mp4
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 600k 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
December 8, 2011 at 14:55 by frasq
Avidemux is a free video editor and converter with a graphical interface available for Linux and Windows. Start with this illustrated manual: Floss Manual on Avidemux.
December 29, 2011 at 15:51 by frasq
The proper format (common denominator) to broadcast a video on the web for all systems:
Container : MP4, headers at the beginning of the file Video : H.264, 480x272 pixels, 600 kbps Audio : AAC, 44.1 kHz stereo, 96 kbpsJanuary 22, 2012 at 15:32 by frasq
To share videos, music or pictures on your network directly from your PC, install ushare:
Edit the file /etc/ushare.conf. Set the parameter USHARE_DIR to the list of directories you want to share.
February 2, 2013 at 13:14 by frasq
minidlna broadcasts .srt files properly and no need to restart it when the content of a directory has changed.
Create a folder /minidlna. Add links to the different folders containing video and audio files. Edit the configuration file /etc/minidlna.conf and add the following lines:
media_dir=A,/minidlna
March 17, 2013 at 15:24 by frasq
To generate a thumbnail for every video, install ffmpegthumbnailer:
Copy the following script in a file called thumbnail in the folder ~/bin:
IFS=$(echo -en "\n\b")
for f in $*
do
ext=`echo -n $f | sed 's/.*\(\..*\)$/\1/'`
name=`basename $f $ext`
dir=`dirname $f`
ffmpegthumbnailer -i $f -o ${dir}/${name}.jpg
done
To add a comment, click here.