Home/IT/Debian/Videos for iPhone  

 

 

It is not possible to import DVDs directly into iTunes to be watched on my iPhone or my wife's iPod Touch. So I must encode our movies on my PC. This is strange as the content industry tries to force me to buy the same movies over and over again. Have you ever bought a book a second time because there is a new type of paper on the market?

This is a report about how to convert a DVD for use in iPhone with Debian. Note that you need a libcss linked version of mplayer.

Procedure

I have to do six steps to convert a movie:

Step 1 - Selecting the title
Step 2 - Selecting the audio track
Step 3 - Dumping to disc
Step 4 - Cropping the geometry
Step 5 - Testing encoding
Step 6 - Encoding

Example 1: The Simpsons

Step 1

Checking the content of the DVD and selecting the title. On this DVD some movies are available in different titles for unknown reason. There are six movies on the disc 3 (season 3) but I can see eight of them. Obviously tracks 01|07, 03|08 and 09|18 are the same. According to the number of chapters the tracks 09 and 18 are no movies at all. I finally select title 01.

$ lsdvd | grep '^Title' | sort -k 3 -r | cut -f 1-2 -d ',' | head
Title: 02, Length: 00:22:08.040 Chapters: 07
Title: 07, Length: 00:22:07.360 Chapters: 07
Title: 01, Length: 00:22:07.360 Chapters: 07
Title: 05, Length: 00:22:07.120 Chapters: 07
Title: 06, Length: 00:22:07.080 Chapters: 06
Title: 04, Length: 00:22:06.090 Chapters: 07
Title: 08, Length: 00:22:05.140 Chapters: 07
Title: 03, Length: 00:22:05.140 Chapters: 07
Title: 18, Length: 00:05:32.100 Chapters: 52
Title: 09, Length: 00:05:32.100 Chapters: 52
$ TITLE=01

Step 2

Checking the audio ID. I found one German language track.

$ mplayer dvd://${TITLE} -ao null -vo null -frames 30 2>&1 | grep aid
audio stream: 0 format: ac3 (5.1) language: en aid: 128.
audio stream: 1 format: ac3 (5.1) language: de aid: 129.
audio stream: 2 format: ac3 (stereo) language: en aid: 130.
$ AID=129

Step 3

Dumping the title to disc and cheking the result.

$ NAME="simpsons-season3-disc3-title1"
$ mplayer dvd://${TITLE} -dumpstream -dumpfile ${NAME}.vob
$ ls -lh ${NAME}.vob
-rw-r--r-- 1 jexss jexss 1.1G 2008-11-23 14:18 simpsons-season3-disc3-title1.vob
$ mplayer -aid ${AID} ${NAME}.vob

Step 4

Cropping. First let's see what is recommended by mplayer's cropdetect:

$ mplayer ${NAME}.vob -ao null -vo null -vf cropdetect=:2 -ss 1000 -frames 30 2>&1 | grep '[CROP]' | tail -1 | cut -f 2 -d '=' | cut -f 1 -d ')'
720:576:0:0

Mostly it is sufficient to just leave this on the recommended value. In case of a noisy outline it may be a good idea to remove one line from all sides of the picture. To do so:

- Crop 16 horizontal pixels
- Move the center of the picture 8 pixels to the right.
- Crop 16 vertical pixels
- Move the center of the picture 8 pixels to the top.

$ echo "720 - 16" | bc
704
$ echo "0 + 8" | bc
8
$ echo "576 - 16" | bc
560
$ echo "0 + 8" | bc
8
$ CROP="704:560:8:8"

Step 5

Encoding the movie (20 seconds test run):

$ rm -f divx2pass.log 
$ mencoder ${NAME}.vob -ss 500 -endpos 20 -aid ${AID} -vf harddup,crop=${CROP} -vf-add scale=640:-2,dsize=640:-2,expand=640:480 -ofps 25 -oac lavc -o ${NAME}.mp4 -of lavf -lavfopts format=mp4 -af lavcresample=44100 -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:vbitrate=1200:keyint=25:vpass=1
$ mencoder ${NAME}.vob -ss 500 -endpos 20 -aid ${AID} -vf harddup,crop=${CROP} -vf-add scale=640:-2,dsize=640:-2,expand=640:480 -ofps 25 -oac lavc -o ${NAME}.mp4 -of lavf -lavfopts format=mp4 -af lavcresample=44100 -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:vbitrate=1200:keyint=25:vpass=2
$ mplayer ${NAME}.mp4

It's important to check the right geometry. My iPhone will play videos with exactly 640x480 pixels only. 

$ mplayer ${NAME}.mp4 -vo null -ao null -endpos 1 2>&1 | grep ^VO
VO: [null] 640x480 => 640x480 Planar YV12

Step 6

Final encoding:

$ rm -f divx2pass.log 
$ mencoder ${NAME}.vob -aid ${AID} -vf harddup,crop=${CROP} -vf-add scale=640:-2,dsize=640:-2,expand=640:480 -ofps 25 -oac lavc -o ${NAME}.mp4 -of lavf -lavfopts format=mp4 -af lavcresample=44100 -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:vbitrate=1200:keyint=25:vpass=1
$ mencoder ${NAME}.vob -aid ${AID} -vf harddup,crop=${CROP} -vf-add scale=640:-2,dsize=640:-2,expand=640:480 -ofps 25 -oac lavc -o ${NAME}.mp4 -of lavf -lavfopts format=mp4 -af lavcresample=44100 -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:vbitrate=1200:keyint=25:vpass=2
$ ls -lh ${NAME}.mp4
-rw-r--r-- 1 jexss jexss 211M 2008-11-23 17:10 simpsons-season3-disc3-title1.mp4

Example 2: Don't Come Knocking

The Wim Wenders movie Don't Come Knocking is a typical Cinema wide screen production.

Step 1

Checking the content of the DVD and selecting the title.

$ lsdvd | grep '^Title' | sort -k 3 -r | cut -f 1-2 -d ',' | head -5 
Title: 04, Length: 01:58:09.010 Chapters: 19
Title: 06, Length: 00:22:09.010 Chapters: 12
Title: 23, Length: 00:05:31.000 Chapters: 02
Title: 08, Length: 00:04:44.100 Chapters: 06
Title: 20, Length: 00:03:55.020 Chapters: 03
$ TITLE=04

Step 2

Checking the audio ID. I found three German language tracks. One is for visually impaired people, one is with comments by Wim Wenders and one is the real audio track.

$ mplayer dvd://${TITLE} -ao null -vo null -frames 30 2>&1 | grep aid
audio stream: 0 format: ac3 (stereo) language: de aid: 128.
audio stream: 1 format: ac3 (5.1) language: en aid: 129.
audio stream: 2 format: ac3 (5.1) language: de aid: 130.
audio stream: 3 format: ac3 (stereo) language: de aid: 131.
$ AID=130

Step 3

Dumping the title to disc and cheking the result.

$ NAME="dont-come-knocking"
$ mplayer dvd://${TITLE} -dumpstream -dumpfile ${NAME}.vob
$ ls -lh ${NAME}.vob
-rw-r--r-- 1 jexss jexss 5.4G 2008-11-21 09:01 dont-come-knocking.vob
$ mplayer -aid $AID ${NAME}.vob

Step 4

Cropping a wide screen picture is complicated. First let's see what is recommended by mplayer's cropdetect:

$ mplayer ${NAME}.vob -ao null -vo null -vf cropdetect=:2 -ss 1000 -frames 30 2>&1 | grep '[CROP]' | tail -1 | cut -f 2 -d '=' | cut -f 1 -d ')'
720:432:0:72

There are two ways to crop: Full screen version or cropped slightly to better fit the iPod's display geometry which is 640 x 480 pixels.

1) 704:432:8:72
Option 1: CROP="704:432:8:72"
2) 600:432:60:72
Option 2: CROP="600:432:60:72", my favorite
3) 500:432:110:72
Option 3: CROP="500:432:110:72"
4) 400:432:160:72
Option 4: CROP="400:432:160:72"

Cropping affects the visible part of the picture: 1) full original geometry, 2) my preferred crop for cinemascope, 3) another way to crop, 4) full 4:3 height. Note that all horizontal values are divided by 1.42 due to anamorphic coding.

Version 1 - Full picture

Leave on detected value 720:432:0:72 but crop 8 pixels fom the original geometry on each side to remove the black frame:

- Crop 16 horizontal pixels
- Move the center of the picture 8 pixels to the right.

$ echo "720 - 16" | bc
704
$ echo "0 + 8" | bc
8
$ CROP="704:432:8:72"

Version 2 - Resized picture

To better fit the height of the iPod display I crop a certain amount of the horizontal line. Note that due do anamorphic coding the real geometry of the original picture is 1024x432 (instead of 720x432). So I recommend something like 600 horizontal pixels (which is around 800 in real) which should be a good compromise:

- Crop 120 horizontal pixels
- Move the center of the picture 60 pixels to the right.

$ echo "720 - 600" | bc
120
$ echo "0 + 60" | bc
60
$ CROP="600:432:60:72"

Step 5

Encoding the movie (20 seconds test run):

$ rm -f divx2pass.log 
$ mencoder ${NAME}.vob -ss 3046 -endpos 20 -aid ${AID} -vf harddup,crop=${CROP} -vf-add scale=640:-2,dsize=640:-2,expand=640:480 -ofps 25 -oac lavc -o ${NAME}.mp4 -of lavf -lavfopts format=mp4 -af lavcresample=44100 -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:vbitrate=1200:keyint=25:vpass=1
$ mencoder ${NAME}.vob -ss 3046 -endpos 20 -aid ${AID} -vf harddup,crop=${CROP} -vf-add scale=640:-2,dsize=640:-2,expand=640:480 -ofps 25 -oac lavc -o ${NAME}.mp4 -of lavf -lavfopts format=mp4 -af lavcresample=44100 -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:vbitrate=1200:keyint=25:vpass=2
$ mplayer ${NAME}.mp4
$ mplayer ${NAME}.mp4 -vo null -ao null -endpos 1 2>&1 | grep ^VO
VO: [null] 640x480 => 640x480 Planar YV12

Step 6

Final encoding:

$ rm -f divx2pass.log 
$ mencoder ${NAME}.vob -aid ${AID} -vf harddup,crop=${CROP} -vf-add scale=640:-2,dsize=640:-2,expand=640:480 -ofps 25 -oac lavc -o ${NAME}.mp4 -of lavf -lavfopts format=mp4 -af lavcresample=44100 -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:vbitrate=1200:keyint=25:vpass=1
$ mencoder ${NAME}.vob -aid ${AID} -vf harddup,crop=${CROP} -vf-add scale=640:-2,dsize=640:-2,expand=640:480 -ofps 25 -oac lavc -o ${NAME}.mp4 -of lavf -lavfopts format=mp4 -af lavcresample=44100 -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:vbitrate=1200:keyint=25:vpass=2
$ ls -lh ${NAME}.mp4
-rw-r--r-- 1 jexss jexss 1.1G 2008-11-23 15:41 dont-come-knocking.mp4