Posts Tagged ‘mencoder’

Splitting and Extracting MPEG video files with MEncoder

Monday, January 2nd, 2012

One of the nice things about MythTV is that it lets me save any broadcast as an unencrypted, DRM-free mpeg file.

I recently found out how to use MEncoder to split and trim those mpeg files into single or multiple clips.

MEncoder is a good tool to use because it’s free (as in both freedom and beer), and runs on all major platforms (there are even pre-built binaries for Mac OSX).

MEncoder has two command line options, -ss and -endpos, which let you define the start or end position of the clip you want to extract.

Unfortunately, the default command doesn’t work with mpeg files.

The work-around is to convert the mpeg file to avi format first:

$ mencoder original.mpeg -ovc lavc -oac lavc -o original.avi

Then, create a copy starting or ending at a given point in time, defined as hour:minute:second using either the -ss or -endpos options.

For example, to extract a clip from the 17 minute 50 second mark to the 57 minute 47 second mark from a one-hour file, these two commands will do the trick:

$ mencoder -ss 00:17:50 -oac copy -ovc copy original.avi -o clip_start.avi
$ mencoder -endpos 00:39:57 -oac copy -ovc copy clip_start.avi -o clip.avi

Note that the -endpos was recalculated for the second command as 39:57, not 57:47.

That’s because the clip_start.avi file is 17 minutes and 50 seconds shorter than the original, and so we need to recalculate the clip end position in terms of the new length.

The file clip.avi contains the clip from 17:50 to 57:47 extracted from the original file, and we can discard the intermediate clip_start.avi file.

It takes two commands because MEncoder seems to ignore the second -ss or -endpos option it finds, and uses just the first one.

It would be nice if it would just let us do this instead:

$ mencoder -ss 00:17:50 -endpos 00:57:47 -oac copy -ovc copy original.avi -o clip.avi

 

DRM-Free DVD Player & Ripper on Mac OS X

Sunday, September 20th, 2009

MPlayer is a terrific DRM-free multimedia player.

It also contains Mencoder, which is a great tool for ripping dvds (among other things).

Mac Ports has two versions available, MPlayer and mplayer-devel but neither of them built properly on my Mac (OS version 10.5.8).

Fortunately, the pre-built binaries at this site worked beautifully.

After downloading and unzipping them, I moved the two binary files, mplayer and mencoder, into /opt/local/bin so they appear in the default command-line path.

Since Apple’s built-in DRM-enforcing DVD player is set to startup when a DVD is inserted, I changed the default action in the System Preferences under Media Preferences to be “Ignore” for the “When you insert a video DVD” option.

Now, I can play any DVD track with mplayer in Terminal, like this:

mplayer dvd://[track number]

MPlayer also supports other playback options like subtitles, etc. (docs related to DVD playback are here).

Ripping a DVD track is more complex, but the docs have good examples to follow.

I wrote this shell script to simplify the process:

#!/bin/sh

# source for mencoder options:
# http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-vcd-dvd.html
# (using 11.8.5.2. NTSC DVD example)

if [ $# -eq 2 ]
then 
  mencoder dvd://$1 -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd:tsaf \
    -vf scale=720:480,harddup -srate 48000 -af lavcresample=48000 \
    -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:\
keyint=18:vstrict=0:acodec=ac3:abitrate=192:aspect=16/9 -ofps 30000/1001 \
    -o $2.mpg
else
  echo "mencoder_dvd_rip.sh [dvd track number] [output filename]"
fi

So to rip track 0 of a DVD (which is usually the main movie track), all I have to do is this in Terminal:

 mencoder_dvd_rip.sh 0 movie