Anime music videos

I wasn’t aware of this before; Anime music videos are main-stream songs with re-mixes (or ‘mash-ups’ if you prefer) various anime as the music-video. This stands to get interesting, as it’s clearly a violation of copyright, but is also clearly creating something new and unique. Laurence Lessig has some interesting thoughts on this:

This will be the next big copyright war — whether this form of noncommercial creativity will be allowed. But there will be a big difference with this war and the last (over p2p filesharing). In the p2p wars, the side that defended innovation free of judicial supervision was right. But when ordinary people heard both sides of the argument, 90% were against us. In this war, the side that will defend these new creators is right. And when ordinary people hear both sides, and more importantly, see the creativity their kids are capable of, 90% will be with us.

Encoding multi-track DVD to Matroska

So while I’ve been off I’ve been hacking around with video-encoding from DVDs. There are various tools for doing what I’m about to describe, but I found problems with them:

  • dvd::rip: Currently only supports OGM as a container. OGM’s nice enough, but development has ceased upstream; it’s really a tech-demo.
  • OGMRip: This is quite nice, but has no preview, and I got audio drift issues (see below for why) and its time estimates are way out.

The main alternatives to OGM are AVI (which is showing its age) and Matroska. The cool thing about Matroska is the ability to embed a lot of meta-information into the files, including attachments and subtitles.

For this example I’m going to encode multiple tracks from the Fellowship of the Ring extended edition, limiting it to chapter 2 for speed.

Encoding is usually referred-to as 3-pass process, but in practice there are 6 steps:

== Extract DVD ==

I’m using dvdbackup. This is decrypts and copies the raw DVD files onto the local filesystem:

dvdbackup -i /dev/dvd -o /tmp -M

This results in the directory /tmp/LOTR_SEE_D1/

== Audio rip/convert ==

We’re going to use mplayer/mencoder to most of the encoding work. So we scan the DVD for audio tracks to get their IDs:

mplayer -v -dvd-device LOTR_SEE_D1/ -frames 0 dvd://1 2> /dev/null |grep aid

This gives the output:


[open] audio stream: 0 audio format: ac3 (5.1) language: en aid: 128
[open] audio stream: 1 audio format: dts (5.1) language: en aid: 137
[open] audio stream: 2 audio format: ac3 (stereo) language: en aid: 130
[open] audio stream: 3 audio format: ac3 (stereo) language: en aid: 131
[open] audio stream: 4 audio format: ac3 (stereo) language: en aid: 132
[open] audio stream: 5 audio format: ac3 (stereo) language: en aid: 133
[open] audio stream: 6 audio format: ac3 (stereo) language: en aid: 134

We can try out these tracks to find out what they are:

mplayer -v -dvd-device LOTR_SEE_D1/ -aid 133 dvd://1

We’ll pick three for now:

  • 130: Plain stereo track
  • 131: Director, writers
  • 134: Cast

To dump them we do:

mplayer -dvd-device LOTR_SEE_D1/ \
	-aid 130 \
	-chapter 2-2 \
	-dumpaudio -dumpfile stereo.dump \
	dvd://1
mplayer -dvd-device LOTR_SEE_D1/ \
	-aid 131 \
	-chapter 2-2 \
	-dumpaudio -dumpfile director.dump \
	dvd://1
mplayer -dvd-device LOTR_SEE_D1/ \
	-aid 134 \
	-chapter 2-2 \
	-dumpaudio -dumpfile cast.dump \
	dvd://1

I prefer Vorbis as an audio codec, so we need to convert these files to OGG:

for i in *.dump; do
    name=`basename $i .dump`
    a52dec -o wav $i > $name.wav
    oggenc -q3 $name.wav
done

(An alternative to this is to stream the audio in PCM format and use a FIFO to join this to the input of oggenc for better parallelism.)

== Crop detect ==

Black borders around the target stream are a waste of bits that could be spent elsewhere, so we want to crop these. Luckily mencoder will do the grunt work on this:

mencoder -nosound -nocache -ovc lavc \
	 -vf cropdetect -o /dev/null \
	 -dvd-device LOTR_SEE_D1/ -chapter 2-2 \
	 dvd://1

This give us the value crop=704:416:8:80.

== Calc bitrate ==

The required bitrate for a given target file-size can be calculated like this:


vbitrate = (target_size - audio_size - overhead) * 8 / duration / 1000

However as we’re just encoding a short sequence we’ll just use 800 as a reasonable value.

== Video encoding Passes ==

This step is well documented elsewhere, although with one caveat: In theory we can just drop the audio from the encoding process as we’ve done that seperately. However in practice this causes the audio and video to go out of sync, so we copy the raw audio stream (with ‘-oac copy’) and then replace it in the final step.

The resulting mencoder commands are:


CROP=704:416:8:80
BITRATE=800
XVIDOPTS=chroma_opt:chroma_me:hq_ac:autoaspect:\
vhq=4:bvhq=1:turbo:trellis:max_bframes=2:\
bitrate=${BITRATE}

## Video Pass 1
mencoder \
    -nocache \
    -nosound \
    -oac copy \
    -noskip \
    -zoom \
    -skiplimit 0 \
    -sws 7 \
    -vf crop=${CROP},hqdn3d=2:1:2,harddup \
    -ovc xvid \
    -passlogfile /tmp/xvid-twopass.stats \
    -xvidencopts ${XVIDOPTS}:pass=1 \
    -o /dev/null \
    -dvd-device /tmp/LOTR_SEE_D1 \
    dvd://1

## Video Pass 2
mencoder \
    -nocache \
    -oac copy \
    -noslices \
    -noskip \
    -zoom \
    -skiplimit 0 \
    -sws 7 \
    -vf crop=${CROP},hqdn3d=2:1:2,harddup \
    -ovc xvid \
    -passlogfile /tmp/xvid-twopass.stats \
    -xvidencopts ${XVIDOPTS}:pass=2 \
    -o /tmp/video.avi \
    -dvd-device /tmp/LOTR_SEE_D1 \
    dvd://1

== Mux all the files into a Matroska container ==

We pull is this together with the mkvmerge command. This adds the video (dropping the audio), the audio (adding meaningful titles), and some basic meta-data:

mkvmerge --title LOTR-FOTR-Test-Encode \
	 -o LOTR-FOTR-Test-Encode.mkv \
	 --default-language eng \
	 --default-track 2 \
	 -A video.avi \
	 -D --track-name 0:"Stereo soundtrack" stereo.ogg \
	 -D --track-name 0:"Director/Writer commentary" director.ogg \
	 -D --track-name 0:"Cast commentary" cast.ogg

There’s more we could do here (I haven’t ripped and added the subtitles from the DVD as it would be awkward in this case), but this is the basic process.

  1. Archives

  2. Categories

  3. Twitter

    • Just got pictures of the earthquake damage from my sister in Kaiapoi. Real "the ground opened up" stuff. 38 minutes ago
    • Ooo, the beta of Angry Birds is out on Android. 18 hours ago
    • Released a new version of my Android Internode widget; fixes the problem with Internode's new SSL cert. 23 hours ago
    • @wangjammer5: Cool, here's something to get you started: http://is.gd/eT33b :) 1 day ago
    • So, if iTune Ping is Apple's social network will the perpetual Apple tweet circle-jerk move there now? 1 day ago
  4. RSS Google Reader Shared Items