FFmpeg DVD

From Indie IT - Wiki
Jump to: navigation, search

NOTICE

The program 'ffmpeg' now has a new fork called 'avconv' which Ubuntu is recommending. Yes. Please use 'avconv' instead... it does a better job.

To this end, I have just done a search-and-replace on this wiki page, replacing most of 'ffmpeg' with 'avconv'...

Introduction

This page will show how to create a DVD and converting different popular video file formats.

Converting File Formats

avconv -i film.avi -target pal-dvd dvd.mpg

DVD Authoring

Make dvd files without menu in a dvd/ folder...

dvdauthor -o dvd/ -t dvd.mpg

Create the table of contents...

dvdauthor -o dvd/ -T

DVD Burning

Now burn the DVD...

growisofs -Z /dev/dvd -dvd-video dvd/

Converting From One Format To Another

LATEST - DECEMBER 2013

Using HandBrake...

MKV

HandBrakeCLI --input input_file.vob --title 1 --markers --encoder x264 --x264-preset veryslow --x264-tune animation --audio 1 --aencoder copy --subtitle 1 --output output_file.mkv

Using avconv...

MKV

avconv -y -i input_file.mkv -s hd720 -f matroska -vcodec libx264 -profile:v main -level 41 -preset veryslow -tune film -crf 22 -threads 0 -b 1152k -metadata title="Movie Title" -metadata:c title="" -metadata:s title="" -acodec ac3 -ab 384k -ac 6 -ar 48000 -sn output_file.mkv

AVI

...

Command line to convert a MKV file to a smaller xVid AVI file, by reducing the video bit rate and audio quality...

avconv -y -i input_file.mkv -f avi -vcodec mpeg4 -vtag xvid -b 1152k -mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300 -acodec mp2 -ab 128k -ac 2 -ar 44100 -threads 1 output_file.avi

Command line to convert a large HD (1280x720) video at 1Gb into a smaller 350Mb video...

avconv -y -i input_file.m4v -s 640x360 -aspect 16:9 -f avi -vcodec mpeg4 -qscale 3 -acodec libmp3lame -ab 128k -ac 2 -ar 44100 output_file.avi

Command line to convert a Full HD mkv video at 17Gb into a smaller HD avi video at 2Gb but keeping excellent quality...

avconv -y -i input_file.mkv -s hd720 -f avi -vcodec libx264 -vpre hq -crf 22 -threads 0 -b 1152k -acodec libmp3lame -ab 128k -ac 2 -ar 44100 output_file.avi

Before and after (the 4 hours of re-encoding [on a Quad-Core with 8Gb of RAM])...

-rw-r--r-- 1 paully paully  17G 2010-08-31 22:43 input_file.mkv
-rw-r--r-- 1 paully paully 2.1G 2010-09-01 05:12 output_file.avi

Using Mplayer On A DVD To Produce A VOB File

mplayer dvd://1 -dumpstream -dumpfile mydvd.vob

Using Mplayer On An ISO To Produce A VOB File

Extract whole film...

mplayer dvd://1 -dvd-device mydvd.iso -dumpstream -dumpfile mydvd.vob

Extract just one chapter...

mplayer dvd://1 -dvd-device mydvd.iso -chapter 1-1 -dumpstream -dumpfile mydvd-1.vob

Extract The Subtitles From a VOB File

mencoder mydvd.vob -nosound -ovc copy -o /dev/null -vobsubout mydvd -sid 0 -vobsuboutindex 0 -vobsuboutid en

Extract Just The Audio Track From A Video File

This command will jump to a specific point in the video then only get 30 seconds of mp3 audio...

(The -vn helps because it tells avconv to ignore the video stream)

avconv -i input_file.avi -ss 00:03:51 -t 00:00:30 -vn -acodec libmp3lame -ab 128k -ac 2 -ar 44100 output_file.mp3

Convert M4A To MP3

for f in *.m4a; do avconv -i "$f" -acodec libmp3lame -ab 320k "${f%.m4a}.mp3"; done

Convert FLAC To MP3

for f in *.flac; do avconv -i "$f" -acodec libmp3lame -ab 320k "${f%.flac}.mp3"; done

Installing the SVN Release of ffmpeg in Ubuntu Linux

Uninstall and Install the necessary software...

sudo apt-get remove ffmpeg x264 libx264-dev
sudo apt-get update
sudo apt-get install build-essential subversion git-core checkinstall yasm texi2html libfaac-dev \
libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libvorbis-dev \
libx11-dev libxfixes-dev libxvidcore-dev zlib1g-dev

Create the necessary new application install directories...

sudo mkdir /usr/local/include
sudo mkdir /usr/local/lib/pkgconfig
sudo mkdir /usr/local/share/man

Install x264 first...

cd
sudo rm -rf x264/
git clone git://git.videolan.org/x264.git
cd x264
./configure --enable-static
make
sudo checkinstall --pkgname=x264 --pkgversion "svn`date +%Y%m%d`+`git rev-list HEAD -n 1 | head -c 7`" --backup=no --default
cd
which x264
x264 --version
echo x264 hold | sudo dpkg --set-selections

Then install ffmpeg...

cd
sudo rm -rf ffmpeg/
git clone git://git.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac \
--enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvorbis \
--enable-libx264 --enable-libxvid --enable-x11grab --enable-pthreads
make
sudo checkinstall --pkgname=ffmpeg --pkgversion "svn`date +%Y%m%d`+`git rev-list HEAD -n 1 | head -c 7`" --backup=no --default
cd
which ffmpeg
ffmpeg
echo ffmpeg hold | sudo dpkg --set-selections

Cropping The Black Bars

You would not believe the lack of SIMPLE, clear documentation with _examples_ on the internet for this!

It took me over 2 hours of mucking around to get this right...

Basically, you resize your picture, crop from the top, and then state your finished picture size.

The magic formula was scaled height - cropped height divided by 2 e.g. 720 - 544 / 2 = 88

It makes perfect sense now, but why has no-one provided examples for dummies by now?!

avconv -y -i input_file.mkv -s 1280X720 -vf crop=0:88:1280:544 -f avi -vcodec libx264 -vpre hq \
-crf 22 -threads 0 -b 1152k -acodec libmp3lame -ab 128k -ac 2 -ar 44100 output_file.avi
-s the scaled picture size to start cropping
-vf the video filter to use
crop=pixels_from_left:pixels_from_top:final_picture_width:final_picture_height

xVid Compatible AVI

avconv -y -i Title_S01E09.vob -f avi -vcodec mpeg4 -vtag xvid -b 1152k -mbd rd -flags +mv4+aic \
-trellis 2 -cmp 2 -subcmp 2 -g 300 -acodec libmp3lame -ab 128k Title_S01E09.avi

avconv: How To Strip Metadata

–map_metadata -1
avconv -i input.mkv -map_metadata -1 -vcodec copy -acodec copy output.mp4

Android and Playstation 3 Compatible MP4

The Sony Playstation 3 is fussy about what video files it can play.

  • Container: MP4
  • Video: H264 AVC Profile Main Level 3.1
  • Audio: AAC
nice -19 avconv -y -i ~/Videos/Film/Film.mkv -map_metadata -1 -s 960x540 -f mp4 -vcodec libx264 -profile:v main -level 31 -preset veryslow -tune film -crf 22 -threads 0 -b 1152k -metadata title="Film" -acodec libvo_aacenc -ab 160k -ac 2 -ar 44100 ~/Videos/Film/Film.mp4

Android Compatible AVI

Don't use this, use the MP4 above.

This will create an .avi movie file suitable for playing with MX Player on HD Android devices.

Example #1 - film, medium speed

avconv -y -i ~/Videos/Film/Film.mkv -s hd720 -f avi -vcodec libx264 -preset medium -tune film -crf 22 -threads 0 -b 1152k -acodec libmp3lame -ab 128k -ac 2 -ar 44100 ~/Videos/Film/Film.avi

Example #2 - animation, slower, clearing stream metadata, forcing profile main and level 3.1

avconv -y -i ~/Videos/Film/Film.mkv -s hd720 -f avi -vcodec libx264 -profile:v main -level 31 -preset slower -tune animation -crf 22 -threads 0 -b 1152k -metadata:s title="" -acodec libmp3lame -ab 128k -ac 2 -ar 44100 ~/Videos/Film/Film.avi

Example #3 - film, very slow, profile main, level 3.1, setting and clearing metadata, plus nice value

nice -19 avconv -y -i ~/Videos/Film/Film.mkv -s hd720 -f avi -vcodec libx264 -profile:v main -level 31 -preset veryslow -tune animation -crf 22 -threads 0 -b 1152k -metadata title="Film" -metadata:c title="" -metadata:s title="" -acodec libmp3lame -ab 128k -ac 2 -ar 44100 ~/Videos/Film/Film.avi

ALSO - you can use cpulimit to stop frying your processor when transcoding...

sudo cpulimit -b -e avconv -l 80

Why do this? You do this because the slower it takes, the better the result. You also finish with a smaller sized file!

If you do this correctly, a 32Gb FullHD MKV movie can come right down to 1Gb.

** This will take about 12 hours so leave it running overnight :-) **

You can read more about the x264 settings here:-

http://mewiki.project357.com/wiki/X264_Settings

x264 Presets

These are the new main x264 presets. In a nutshell, the slower the encoding produces a better quality and smaller size file.

  • ultrafast
  • superfast
  • veryfast
  • faster
  • fast
  • medium (32 fps)
  • slow (16 fps)
  • slower (8 fps)
  • veryslow (4 fps)
  • placebo (2 fps)

** UPDATE - Monday, April 25 2011 **

You call the x264 presets with ffpmpeg differently now.

(OLD METHOD)   -vcodec libx264 -vpre veryslow
(NEW METHOD)   -vcodec libx264 -preset veryslow -tune film

MythTV To AVI Script

Transcode a MythTV recording into an xvid .avi with the title and subtitle as filename.

Put this in /usr/local/bin/ and create a new MythTV User Job called 'Export to AVI' with the command input line:

myththto.sh %FILE%

Latest version: 2012-08-11: http://wiki.indie-it.com/images/5/5f/Mythtvto.sh

MKV To AVI Script

#!/bin/bash
#
#    mkvtoavi.sh
#
#    Description: convert a really big Full HD Matroska video file into a much smaller Standard HD Audio Video Interleave file.
#
#    Details: Conversion will take 4 x length of movie using the X264 codec at 'veryslow' preset.
#             For an average movie this will mean about 8 hours - so leave it running overnight!
#
#    Usage: ./mkvtoavi.sh input.file output.file
#
INPUTFILE="$1"
X264PRESET="veryslow"    # (superfast,veryfast,faster,fast,medium,slow,slower,veryslow,placebo)
OUTPUTFILE="$2"
if [ ! $# == 2 ]
  then echo "Usage: ./mkvtoavi.sh input.file output.file"
  else time avconv -y -i "$INPUTFILE" -s hd720 -f avi -vcodec libx264 -preset "$X264PRESET" -crf 22 -threads 0 -b 1152k -acodec libmp3lame -ab 128k -ac 2 -ar 44100 "$OUTPUTFILE"
fi

Mencoder

UPDATE: 2013-05-16

  1. http://www.axllent.org/code/divxcalc/
  2. http://quadpoint.org/projects/simplerip/

Check the correct tile track of a DVD...

lsdvd

Two pass encode with target size of 700Mb...

mencoder dvd://1 -alang en -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=128 -xvidencopts pass=1 -o /dev/null
mencoder dvd://1 -alang en -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=128 -xvidencopts pass=2:bitrate=-700000 -o output.avi

Two pass encode with black bar cropping of a 2.35:1 Widescreen UK PAL 720x576 picture and fixed bitrate for smaller final size...

mplayer dvd://1 -chapter 3 -vf cropdetect
mencoder dvd://1 -vf crop=720:416:0:80,scale=640:256 -ovc xvid -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=1 -oac copy -o /dev/null
mencoder dvd://1 -vf crop=720:416:0:80,scale=640:256 -ovc xvid -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=2 -alang en -oac mp3lame -lameopts br=96:cbr:vol=6 -o output.avi

Same, but for a 16:9 Widescreen UK PAL 720x576 picture...

mencoder dvd://1 -alang en -vf crop=720:410:0:80,scale=720:400 -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=96:vol=6 -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=1 -o /dev/null
mencoder dvd://1 -alang en -vf crop=720:410:0:80,scale=720:400 -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=96:vol=6 -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=2 -o output.avi 

Same, but for a 4:3 Standard Definition UK PAL picture...

mencoder dvd://1 -alang en -vf crop=688:560:18:8,scale=640:480 -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=96:vol=6 -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=1 -o /dev/null
mencoder dvd://1 -alang en -vf crop=688:560:18:8,scale=640:480 -vobsub 999 -ovc xvid -oac mp3lame -lameopts cbr:br=96:vol=6 -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=2 -o output.avi 

http://ubuntuguide.org/wiki/Video_Conversion

http://www.axllent.org/docs/video/mencoder-dvd-to-mpeg4

Transcode

Example #1

Create an XviD .avi video of the 5th chapter of the 3rd title of a DVD ISO file, reducing the picture size to 640x360, whilst keeping the 16:9 aspect ratio...

transcode -i ~/Videos/Being_Human_S01D01.iso -T 3,5 -Z 640x360 -y xvid4,tcaud -o ~/Videos/Being_Human_S01E01-5.avi

-i = input filename. -T = title and chapter. -Z = resize video. -y = output format. -o = output filename.

Example #2

Create an XviD .avi video of a MythTV recording, using mplayer to probe for video and audio types, just use the first 2 streams for video and audio, reducing the picture size to 640x360, whilst keeping the 16:9 aspect ratio...

transcode -q 0 -i 7335_20110329090000.mpg --mplayer_probe -S 0,0-1 -Z 640x360 -y xvid4,tcaud -o /var/lib/mythtv/exports/test.avi

-q = debug level - quiet. --mplayer_probe = use mplayer. -S = stream and channel(s). -i = input filename. -T = title and chapter. -Z = resize video. -y = output format. -o = output filename.

Troubleshooting

Error...

[export_xvid4.so] warning: Usage of this module for audio encoding is deprecated.
[export_xvid4.so] warning: Consider switch to export_tcaud module.

Fix...

-y xvid4,tcaud

Full example...

trancode - i /dev/dvd -y xvid4,tcaud -o /path/to/filename.avi

ffmpeg Audio Channel Layouts

-channel_layout
7.1: Front: L C R, Surround: L R, Back: L R, LFE
6.1: Front: L C R, Surround: L R, Back: C, LFE
5.1: Front: L C R, Surround: L R, LFE 
layout_map[] = {
   {  1, CHANNEL_LAYOUT_MONO                                 , "mono"        },
   {  2, CHANNEL_LAYOUT_STEREO                               , "stereo"      },
   {  4, CHANNEL_LAYOUT_QUAD                                 , "quad"        },
   {  6, CHANNEL_LAYOUT_5POINT1                              , "5.1"         },
   {  8, CHANNEL_LAYOUT_5POINT1|CHANNEL_LAYOUT_STEREO_DOWNMIX, "5.1+downmix" },
   {  8, CHANNEL_LAYOUT_7POINT1                              , "7.1"         },
   {  8, CHANNEL_LAYOUT_7POINT1_WIDE                         , "7.1(wide)"   },
   { 10, CHANNEL_LAYOUT_7POINT1|CHANNEL_LAYOUT_STEREO_DOWNMIX, "7.1+downmix" },
   {  0 }

Join MP4 Files Together

Install GPAC first, easily from ubuntu repository:

sudo apt-get install gpac

Then using one of GPAC's tool, named MP4Box, we can join or merge multiple mp4 files by concatenating into one file. With concatenating we save a huge amount of processing time which is usually spent for rendering/re-encoding.

MP4Box -cat file1.mp4 -cat file2.mp4 -cat file3.mp4 OutputFile.mp4

HOWTO: FIX: ERROR: avconv: libmp3lame not found

sudo apt-get install libavcodec-extra-53