Manual video encoding

(transcode)
(Undo vandalism)
 
(19 intermediate revisions not shown)
Line 1: Line 1:
 +
__FORCETOC__
For automatic video conversion tools, see the [[video encoding]] article.
For automatic video conversion tools, see the [[video encoding]] article.
== Mencoder ==
== Mencoder ==
-
The tool I use to encode video is mencoder that comes with mplayer on Linux. Always start with the cleanest video you can, and make sure it has good A/V sync before you start by watching it on a PC. Make sure the original has a frame rate greater than or equal to the one you are trying to generate.  
+
One tool you can use to encode video is mencoder that comes with mplayer on Linux. Always start with the cleanest video you can, and make sure it has good A/V sync before you start by watching it on a PC. Make sure the original has a frame rate greater than or equal to the one you are trying to generate.  
The recommended way to run mencoder is through tablet-encoder. You can however try running mencoder directly and playing with the options. Be careful about forcing the output frame rate, however. I didn't get any good result with the below commands. Instead, I got A/V desync and bad effects due do frame rate changes, so I now use some variation of the above shell scripts.
The recommended way to run mencoder is through tablet-encoder. You can however try running mencoder directly and playing with the options. Be careful about forcing the output frame rate, however. I didn't get any good result with the below commands. Instead, I got A/V desync and bad effects due do frame rate changes, so I now use some variation of the above shell scripts.
-
  mencoder infile.avi -oac mp3lame -ovc lavc -lavcopts vcodec=mpeg4 -vf scale=352:208 -ffourcc DIVX -ofps 15 -o outfile.avi
+
  mencoder infile.avi -oac mp3lame -ovc lavc \
 +
  -lavcopts vcodec=mpeg4 -vf scale=352:208 -ffourcc DIVX \
 +
  -ofps 15 -o outfile.avi
This will generate a 15fps 352x208 avi file.
This will generate a 15fps 352x208 avi file.
Line 13: Line 16:
The following mencoder command line will make smaller videos:
The following mencoder command line will make smaller videos:
-
  mencoder infile.avi -oac mp3lame -ovc lavc -lavcopts vcodec=mpeg4:mbd=1:vbitrate=300 -vf scale=352:208 -ffourcc DIVX -ofps 15 -o outfile.avi
+
  mencoder infile.avi -oac mp3lame -ovc lavc \
 +
  -lavcopts vcodec=mpeg4:mbd=1:vbitrate=300 -vf scale=352:208 \
 +
  -ffourcc DIVX -ofps 15 -o outfile.avi
 +
 
 +
mplayer will play 30 fps video fine.  The following command produces acceptable video with no A/V sync issues.
 +
 
 +
mencoder infile.avi -oac mp3lame -ovc lavc \
 +
  -lavcopts vcodec=mpeg4:mbd=1:vbitrate=300 -vf scale=352:208 \
 +
  -ffourcc DIVX -o outfile.avi
 +
 
 +
The above command was tested on 30 fps source file and creates 30 fps video that plays back fine on the N810.
To make even smaller, you can drop down the quality of audio to 64 kbit/s (for 300 kbit video stream, converting audio to 64 kbit versus 128 kbit yields a total save of approx 20%. Notice that for lower bitrate movies percentage is considerably higher):
To make even smaller, you can drop down the quality of audio to 64 kbit/s (for 300 kbit video stream, converting audio to 64 kbit versus 128 kbit yields a total save of approx 20%. Notice that for lower bitrate movies percentage is considerably higher):
-
  mencoder infile.avi -oac mp3lame -lameopts abr:br=64 -ovc lavc -lavcopts vcodec=mpeg4:mbd=1:vbitrate=300 -vf scale=352:208 -ffourcc DIVX -ofps 15 -o outfile.avi
+
  mencoder infile.avi -oac mp3lame -lameopts abr:br=64 -ovc lavc \
 +
  -lavcopts vcodec=mpeg4:mbd=1:vbitrate=300 -vf scale=352:208 \
 +
  -ffourcc DIVX -ofps 15 -o outfile.avi
 +
 
 +
The differences are the block detection method (mbd=1) and a capped bitrate.
-
The differences are the block detection method (mbd=1) and a capped bitrate see man mencoder for more options
+
See man mencoder for more options.
== ffmpeg ==
== ffmpeg ==
-
It is possible to transcode a video with ffmpeg. But the video will have a corrupted fourcc header. This can be corrected with the tool avifix from transcode or by using -vtag DIVX in ffmpeg command line:
+
It is possible to transcode a video with ffmpeg. The video will have a corrupted fourcc header, but this can be corrected with the tool avifix from transcode or by using -vtag DIVX in ffmpeg command line:
  ffmpeg -i infile -vcodec mpeg4 -acodec mp3 -s 176:144 -b 200 -pass 1 outfile.avi
  ffmpeg -i infile -vcodec mpeg4 -acodec mp3 -s 176:144 -b 200 -pass 1 outfile.avi
Line 33: Line 50:
  ffmpeg -i infile -vcodec mpeg4 -acodec mp3 -vtag DIVX -s 176:144 -b 200 -pass 1 outfile.avi
  ffmpeg -i infile -vcodec mpeg4 -acodec mp3 -vtag DIVX -s 176:144 -b 200 -pass 1 outfile.avi
  ffmpeg -i infile -vcodec mpeg4 -acodec mp3 -vtag DIVX -s 176:144 -b 200 -pass 2 outfile.avi
  ffmpeg -i infile -vcodec mpeg4 -acodec mp3 -vtag DIVX -s 176:144 -b 200 -pass 2 outfile.avi
 +
 +
The N900 supports AAC and H.264 up to Baseline 3.0 profile. This produces quite good quality, but takes a while to encode:
 +
 +
ffmpeg -i input.avi -threads 0 -vcodec libx264 -acodec libfaac -vpre hq -vpre baseline -ab 128k -b 600k output.mp4
 +
 +
===ffmpeg 0.7.0===
 +
ffmpeg 0.7.0 require different parameters to produce a Baseline 3.0 video file that the N900 will play.
 +
ffmpeg -threads 0 -i input.mkv -vcodec libx264 -s 852x480 -b 1200k -ac 2 -ab 160k -flags +loop+mv4 -cmp 256 -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -me_method umh -subq 7 -refs 5 -me_range 16 -g 250 -qmin 10 -qmax 51 -vpre baseline -f mp4 output.mp4
 +
 +
===ffmpeg 0.10.3===
 +
ffmpeg 0.10.3 requires different parameters.  The previous command should be written as:
 +
ffmpeg -threads 0 -i input.mkv -vcodec libx264 -s 852x480 -b:v 1200k -ac 2 -b:a 160k -flags +loop+mv4 -cmp 256 -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -me_method umh -subq 7 -refs 5 -me_range 16 -g 250 -qmin 10 -qmax 51 -vprofile baseline -f mp4 output.mp4
== transcode ==
== transcode ==
-
I just converted a DVD using transcode using the following command line:
+
You can convert a DVD using transcode using the following command line:
  transcode -i /dev/dvd \
  transcode -i /dev/dvd \
Line 50: Line 79:
           -o output.avi
           -o output.avi
-
This transcodes chapters 1 to 16 of title 1. The DVD has 720x576, but uses only 720x480, so I clipped off the remaining 96 black pixels with the -j option. Output size is 240x160. Video bitrate is 300kbps, audio is 48kbps. The "xvid" codec (a symlink to xvid2 on my system) gives very good results with the default settings. Do not use "mpeg4", it needs too much processor power for decoding which causes loads of artefacts when playing on the Nokia, even if it looks good on a PC.
+
This transcodes chapters 1 to 16 of title 1. The DVD has 720x576, but uses only 720x480, so the command clips off the remaining 96 black pixels with the -j option. Output size is 240x160. Video bitrate is 300kbps, audio is 48kbps. The "xvid" codec (a symlink to xvid2 on some systems) gives very good results with the default settings. Do not use "mpeg4", it needs too much processor power for decoding which causes loads of artefacts when playing on the Nokia, even if it looks good on a PC.
-
I used the transcode package with Debian unstable.
+
Fix the FOURCC with
-
 
+
-
afterwards, I fixed the FOURCC with
+
  avifix -i output.avi -F DIVX
  avifix -i output.avi -F DIVX
-
The resulting AVI file has 114 MByte for 45 minutes/25fps. Things can certainly be improved by tuning xvid options using a ./xvid2.cfg, but I didn't try that yet.
+
The resulting AVI file has 114 MByte for 45 minutes/25fps. Things can certainly be improved by tuning xvid options using a ./xvid2.cfg.
-
Here is another example, where the DVD has 720x576 fully used for 4:3 content. I chose 256x192 as output size (4x64, 3x64). This time I used the xvid4 codec. Note that you must not use B-frames, the 770/N800 doesn't seem to be able to handle them. You can turn them off by setting max_bframes=0 in your xvid4.cfg, you can easily do this with the xvid4conf tool. After you did this, the following command line will do the rest:
+
Here is another example, where the DVD has 720x576 fully used for 4:3 content. The output size is 256x192 (4x64, 3x64). This example uses the xvid4 codec. Note that you must not use B-frames, the 770/N800 doesn't seem to be able to handle them. You can turn them off by setting max_bframes=0 in your xvid4.cfg, you can easily do this with the xvid4conf tool. After you did this, the following command line will do the rest:
  transcode -i /dev/dvd \
  transcode -i /dev/dvd \
Line 73: Line 100:
           -o output.avi
           -o output.avi
-
The -T 1,-1 option will convert the whole title 1 (all chapters). You can try a lower video bitrate by adjusting the -w parameter (300kbps in this example). Don't forget to fix the FOURCC with the avifix command (as above)! The quality is very good, and it works fine with 25fps.
+
The -T 1,-1 option will convert the whole title 1 (all chapters). You can try a lower video bitrate by adjusting the -w parameter (300kbps in this example). Don't forget to fix the FOURCC with the avifix command (as above). The quality is very good, and it works fine with 25fps.
Additional informations regarding transcode and IT 2006 beta :
Additional informations regarding transcode and IT 2006 beta :
Line 109: Line 136:
using ffmpeg, there is no need to fix FOURCC. -R x is used to do two-pass encoding. --keep_asr will resize source video to fit in the specified target resolution, no need to worry about cropping or adding black borders if source isn't using the same aspect ratio (DVD often uses 1.77 anamorphic format ), transcode will take care of it. The only choice to do is target resolution : 320x208 or 240x144 for 16:9 source, 352x288 or 176x144 for 4:3 source.
using ffmpeg, there is no need to fix FOURCC. -R x is used to do two-pass encoding. --keep_asr will resize source video to fit in the specified target resolution, no need to worry about cropping or adding black borders if source isn't using the same aspect ratio (DVD often uses 1.77 anamorphic format ), transcode will take care of it. The only choice to do is target resolution : 320x208 or 240x144 for 16:9 source, 352x288 or 176x144 for 4:3 source.
 +
 +
== H.264 (with mencoder) ==
 +
 +
Both OS2008 (N800/N810) and Maemo 5 (N900) play H.264 videos out of the box - in theory. A lot of existing H.264-material will not play well, though. [http://talk.maemo.org/showthread.php?t=13514&page=4 This thread] on talk.maemo.org indicates that a video file must adhere to the Baseline Profile, level 1.2, of the H.264-standard to be compatible with both the Maemo Media Player and MPlayer. (See Wikipedia for a detailed information on [http://en.wikipedia.org/wiki/H.264#Profiles profiles] and [http://en.wikipedia.org/wiki/H.264#Levels levels].)
 +
 +
The N900 has the same restrictions. [https://bugs.maemo.org/show_bug.cgi?id=6635 This bug report] tells us that with Maemo 5, H.264-videos still must be Baseline.
 +
 +
With mencoder, the following settings are known to work. (Simplified; you may use 2-pass etc.) They are originally taken and adapted from a website about H.264 on the N95 and given here only as an example for those who want to experiment with manual settings.
 +
 +
 +
## Create video
 +
#  I omitted various filters I use to make the video smaller
 +
#  while encoding. A video resolution of 352:208 is assumed here,
 +
#  you can achieve it by adding something like
 +
#  -vf dsize=15/9,scale=352:208
 +
#  Whats important is the part after -x264encopts:
 +
#  * bitrate=300: max would be 364 according to level 1.2
 +
#  * nocabac: no CABAC Entropy Coding according to Baseline profile
 +
#  * partitions=p8×8,b8×8,i4×4: no idea what this is, but didn't work
 +
#    with different settings
 +
#  * level_idc=12: indicate to decoder that we're at level 1.2
 +
#  * frameref=2: influences peak signal to noise ratio (?)
 +
#  * me=umh: motion estimation search method
 +
#  * bframes=0: no B-frames according to basline profile
 +
#  * nodeblock: (?)
 +
#  * noweightb: (?)
 +
#  * nomixed_refs: unknown, but didn't work without it
 +
#  so:
 +
mencoder $INPUTFILE  \
 +
    -nosound \
 +
    -ovc x264 \
 +
   -x264encopts bitrate=300:nocabac:partitions=p8×8,b8×8,i4×4:level_idc=12:frameref=2:me=umh:bframes=0:nodeblock:noweightb:nomixed_refs \
 +
    -of rawvideo -o N800.264
 +
#
 +
#
 +
## Create audio
 +
#  The standard is picky about valid container formats for h.264
 +
#  and audio codecs within these containers.
 +
#  AAC is a good choice; bitrate settings may be too conservative
 +
mencoder $INPUTFILE  \
 +
-ovc copy \
 +
-oac faac -faacopts br=40:mpeg=4:object=2 \
 +
-af resample=32000:0:2,volume=4:1 \
 +
-of rawaudio -o N800.aac
 +
#
 +
## Mux MPEG4 file
 +
#  MP4Box is part of the GPAC package.
 +
#  You could also use mp4creator,
 +
#  but that's buggy and no longer actively developed
 +
#  -fps ist frames per second; you need to give this info
 +
MP4Box -new -add N800.264 -add N800.aac -fps 25.000 N800.mp4
 +
 +
 +
The thread on talk.maemo.org gives an idea how other software may work.
 +
 +
 +
 +
[[Category:Users]]
 +
[[Category:Media]]
 +
[[Category:Power users]]

Latest revision as of 07:08, 3 May 2013

For automatic video conversion tools, see the video encoding article.

Contents

[edit] Mencoder

One tool you can use to encode video is mencoder that comes with mplayer on Linux. Always start with the cleanest video you can, and make sure it has good A/V sync before you start by watching it on a PC. Make sure the original has a frame rate greater than or equal to the one you are trying to generate.

The recommended way to run mencoder is through tablet-encoder. You can however try running mencoder directly and playing with the options. Be careful about forcing the output frame rate, however. I didn't get any good result with the below commands. Instead, I got A/V desync and bad effects due do frame rate changes, so I now use some variation of the above shell scripts.

mencoder infile.avi -oac mp3lame -ovc lavc \
 -lavcopts vcodec=mpeg4 -vf scale=352:208 -ffourcc DIVX \
 -ofps 15 -o outfile.avi

This will generate a 15fps 352x208 avi file.

The following mencoder command line will make smaller videos:

mencoder infile.avi -oac mp3lame -ovc lavc \
 -lavcopts vcodec=mpeg4:mbd=1:vbitrate=300 -vf scale=352:208 \
 -ffourcc DIVX -ofps 15 -o outfile.avi

mplayer will play 30 fps video fine. The following command produces acceptable video with no A/V sync issues.

mencoder infile.avi -oac mp3lame -ovc lavc \
 -lavcopts vcodec=mpeg4:mbd=1:vbitrate=300 -vf scale=352:208 \
 -ffourcc DIVX -o outfile.avi

The above command was tested on 30 fps source file and creates 30 fps video that plays back fine on the N810.

To make even smaller, you can drop down the quality of audio to 64 kbit/s (for 300 kbit video stream, converting audio to 64 kbit versus 128 kbit yields a total save of approx 20%. Notice that for lower bitrate movies percentage is considerably higher):

mencoder infile.avi -oac mp3lame -lameopts abr:br=64 -ovc lavc \
 -lavcopts vcodec=mpeg4:mbd=1:vbitrate=300 -vf scale=352:208 \
 -ffourcc DIVX -ofps 15 -o outfile.avi

The differences are the block detection method (mbd=1) and a capped bitrate.

See man mencoder for more options.

[edit] ffmpeg

It is possible to transcode a video with ffmpeg. The video will have a corrupted fourcc header, but this can be corrected with the tool avifix from transcode or by using -vtag DIVX in ffmpeg command line:

ffmpeg -i infile -vcodec mpeg4 -acodec mp3 -s 176:144 -b 200 -pass 1 outfile.avi
ffmpeg -i infile -vcodec mpeg4 -acodec mp3 -s 176:144 -b 200 -pass 2 outfile.avi
avifix -i outfile.avi -F "divx"

Or directly (without avifix):

ffmpeg -i infile -vcodec mpeg4 -acodec mp3 -vtag DIVX -s 176:144 -b 200 -pass 1 outfile.avi
ffmpeg -i infile -vcodec mpeg4 -acodec mp3 -vtag DIVX -s 176:144 -b 200 -pass 2 outfile.avi

The N900 supports AAC and H.264 up to Baseline 3.0 profile. This produces quite good quality, but takes a while to encode:

ffmpeg -i input.avi -threads 0 -vcodec libx264 -acodec libfaac -vpre hq -vpre baseline -ab 128k -b 600k output.mp4

[edit] ffmpeg 0.7.0

ffmpeg 0.7.0 require different parameters to produce a Baseline 3.0 video file that the N900 will play.

ffmpeg -threads 0 -i input.mkv -vcodec libx264 -s 852x480 -b 1200k -ac 2 -ab 160k -flags +loop+mv4 -cmp 256 -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -me_method umh -subq 7 -refs 5 -me_range 16 -g 250 -qmin 10 -qmax 51 -vpre baseline -f mp4 output.mp4

[edit] ffmpeg 0.10.3

ffmpeg 0.10.3 requires different parameters. The previous command should be written as:

ffmpeg -threads 0 -i input.mkv -vcodec libx264 -s 852x480 -b:v 1200k -ac 2 -b:a 160k -flags +loop+mv4 -cmp 256 -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -me_method umh -subq 7 -refs 5 -me_range 16 -g 250 -qmin 10 -qmax 51 -vprofile baseline -f mp4 output.mp4

[edit] transcode

You can convert a DVD using transcode using the following command line:

transcode -i /dev/dvd \
         -x dvd \
         -T 1,1-16 \
         -a 0 
         -j 48,0,48,0 \
         -Z 240x160 \
         -y xvid \
         -V -w 300 \
         -N 0x55 \
         -b 48 \
         -o output.avi

This transcodes chapters 1 to 16 of title 1. The DVD has 720x576, but uses only 720x480, so the command clips off the remaining 96 black pixels with the -j option. Output size is 240x160. Video bitrate is 300kbps, audio is 48kbps. The "xvid" codec (a symlink to xvid2 on some systems) gives very good results with the default settings. Do not use "mpeg4", it needs too much processor power for decoding which causes loads of artefacts when playing on the Nokia, even if it looks good on a PC.

Fix the FOURCC with

avifix -i output.avi -F DIVX

The resulting AVI file has 114 MByte for 45 minutes/25fps. Things can certainly be improved by tuning xvid options using a ./xvid2.cfg.

Here is another example, where the DVD has 720x576 fully used for 4:3 content. The output size is 256x192 (4x64, 3x64). This example uses the xvid4 codec. Note that you must not use B-frames, the 770/N800 doesn't seem to be able to handle them. You can turn them off by setting max_bframes=0 in your xvid4.cfg, you can easily do this with the xvid4conf tool. After you did this, the following command line will do the rest:

transcode -i /dev/dvd \
         -x dvd \
         -T 1,-1 \
         -a 0 
         -Z 256x192 \
         -y xvid4 \
         -V -w 300 \
         -N 0x55 \
         -b 48 \
         -o output.avi

The -T 1,-1 option will convert the whole title 1 (all chapters). You can try a lower video bitrate by adjusting the -w parameter (300kbps in this example). Don't forget to fix the FOURCC with the avifix command (as above). The quality is very good, and it works fine with 25fps.

Additional informations regarding transcode and IT 2006 beta :

I suggest using the following command-line :

transcode -i /dev/dvd \
         -x dvd \
         -T 1,-1 \
         -a 0 
         -Z 352x208 \
         --keep_asr \
         -y ffmpeg \
         -F mpeg4 \
         -w 300 \
         -N 0x55 \
         -b 48 \
         -R 1 \
         -o output.avi

and

transcode -i /dev/dvd \
         -x dvd \
         -T 1,-1 \
         -a 0 
         -Z 352x208 \
         --keep_asr \
         -y ffmpeg \
         -F mpeg4 \
         -w 300 \
         -N 0x55 \
         -b 48 \
         -R 2 \
         -o output.avi

using ffmpeg, there is no need to fix FOURCC. -R x is used to do two-pass encoding. --keep_asr will resize source video to fit in the specified target resolution, no need to worry about cropping or adding black borders if source isn't using the same aspect ratio (DVD often uses 1.77 anamorphic format ), transcode will take care of it. The only choice to do is target resolution : 320x208 or 240x144 for 16:9 source, 352x288 or 176x144 for 4:3 source.

[edit] H.264 (with mencoder)

Both OS2008 (N800/N810) and Maemo 5 (N900) play H.264 videos out of the box - in theory. A lot of existing H.264-material will not play well, though. This thread on talk.maemo.org indicates that a video file must adhere to the Baseline Profile, level 1.2, of the H.264-standard to be compatible with both the Maemo Media Player and MPlayer. (See Wikipedia for a detailed information on profiles and levels.)

The N900 has the same restrictions. This bug report tells us that with Maemo 5, H.264-videos still must be Baseline.

With mencoder, the following settings are known to work. (Simplified; you may use 2-pass etc.) They are originally taken and adapted from a website about H.264 on the N95 and given here only as an example for those who want to experiment with manual settings.


## Create video 
#  I omitted various filters I use to make the video smaller
#  while encoding. A video resolution of 352:208 is assumed here,
#  you can achieve it by adding something like 
#  -vf dsize=15/9,scale=352:208
#  Whats important is the part after -x264encopts:
#  * bitrate=300: max would be 364 according to level 1.2
#  * nocabac: no CABAC Entropy Coding according to Baseline profile
#  * partitions=p8×8,b8×8,i4×4: no idea what this is, but didn't work 
#    with different settings
#  * level_idc=12: indicate to decoder that we're at level 1.2
#  * frameref=2: influences peak signal to noise ratio (?)
#  * me=umh: motion estimation search method
#  * bframes=0: no B-frames according to basline profile
#  * nodeblock: (?)
#  * noweightb: (?)
#  * nomixed_refs: unknown, but didn't work without it
#  so:
mencoder $INPUTFILE  \
   -nosound \
   -ovc x264 \
   -x264encopts bitrate=300:nocabac:partitions=p8×8,b8×8,i4×4:level_idc=12:frameref=2:me=umh:bframes=0:nodeblock:noweightb:nomixed_refs \
   -of rawvideo -o N800.264
#
#
## Create audio
#  The standard is picky about valid container formats for h.264
#  and audio codecs within these containers.
#  AAC is a good choice; bitrate settings may be too conservative
mencoder $INPUTFILE  \
	-ovc copy \
	-oac faac -faacopts br=40:mpeg=4:object=2 \
	-af resample=32000:0:2,volume=4:1 \
	-of rawaudio -o N800.aac
#
## Mux MPEG4 file
#  MP4Box is part of the GPAC package.
#  You could also use mp4creator, 
#  but that's buggy and no longer actively developed
#  -fps ist frames per second; you need to give this info
MP4Box -new -add N800.264 -add N800.aac -fps 25.000 N800.mp4


The thread on talk.maemo.org gives an idea how other software may work.