## Split, cut, or sample a video file on the command line

There are many reasons to cut or split a video file. For example, one may want to cut a long video into multiple parts to upload to YouTube. I first ran into this and this, which suggests:

 <pre class="src src-sh">ffmpeg -ss 00:00:00 -t 00:01:00 -vcodec copy -acodec copy -i in.avi out.avi


## -ss: start position ## -t: end position ## can re-encode with other codecs

However, for some reason my out file is almost as large as my in file, even though I’m only sampling 1 minute out of the 2 hour segment. This lead me to the mencoder solution:

 <pre class="src src-sh">mencoder -ss 00:00:00 -endpos 00:00:01 -ovc copy -oac copy in.avi -o out.avi


The file size of my out file is more reasonable, and the speed is incredibly fast since I am not re-encoding.

## Rip dvd’s (iso’s) to high quality avi

I recently needed to rip some videos from dvd’s so I can have them on my computer in smaller files. I prefer the command line, so I use MEncoder that comes with mplayer. Supposedly AcidRip is a good interface that automates things for MEncoder. I wrote some scripts based on my research into how to do rip dvd’s from the command line. Note that I have -dvd-device /path/to/iso in all of the scripts since I’m doing videos from an iso file. If you are doing it from disc, remove this portion of the commands.

dvdiso2xvid.sh (one pass, based on xvid):

<pre class="src src-sh"><span style="color: #ff4500;">#</span><span style="color: #ff4500;">! /bin/</span><span style="color: #00ffff;">bash</span>


## Usage: dvdiso2xvid.sh file.iso ## http://www.howforge.com/how-to-encode-dvd-to-avi-using-mencoder ## http://forum.videohelp.com/threads/288190-(SOLVED)-Need-mencoder-help-certain-files-have-no-audio for file in “$@” do bn=basename "$file" NameNoExt=${bn%.*} ## no extension mencoder dvd://1 -dvd-device “$bn” -oac mp3lame -lameopts mode=2:cbr:br=128:vol=0 -ovc xvid -xvidencopts bitrate=-1 -vf scale -zoom -xy 480 -o “$NameNoExt.avi” done dvdiso2lavc.sh (one pass, based on libavcodec): <pre class="src src-sh"><span style="color: #ff4500;">#</span><span style="color: #ff4500;">! /bin/</span><span style="color: #00ffff;">bash</span>  ## Usage: dvdiso2lavc.sh file.iso ## http://www.howforge.com/how-to-encode-dvd-to-avi-using-mencoder ## http://forum.videohelp.com/threads/288190-(SOLVED)-Need-mencoder-help-certain-files-have-no-audio for file in “$@” do bn=basename "$file" NameNoExt=${bn%.*} ## no extension mencoder dvd://1 -dvd-device “$bn” -oac mp3lame -lameopts mode=2:cbr:br=128:vol=0 -ovc lavc -lavcopts vcodec=mpeg4:vhq:v4mv:vqmin=2 -o “$NameNoExt.avi” done

dvdiso2xvid_2pass.sh (two passes (so better quality), crop images (for use with widescreen), based on xvid):

<pre class="src src-sh"><span style="color: #ff4500;">#</span><span style="color: #ff4500;">! /bin/</span><span style="color: #00ffff;">bash</span>


VBR=1000 ## higher, better quality ABR=128 ## 96 recommended for file in “$@” do rm -f divx2pass.log bn=basename "$file" NameNoExt=${bn%.*} ## no extension # start a timer to kill mplayer (sleep 5 && killall mplayer)& # start the mplayer cropdetect on DVD’s chapter 4 mplayer dvd://1 -dvd-device “$bn” -chapter 3 -vf cropdetect &> mplayer.tmp # get last crop value from mplayer output and store in variable CROP_VALUES=$(awk -F‘crop=’ ‘/[CROP]/{f=$2} END{print f}’ ./mplayer.tmp |cut -d‘)’ -f1) # print detected crop values echo -e “nnDetected crop values = ${CROP_VALUES}nn” mencoder dvd://1 -dvd-device “$bn” -vf crop=${CROP_VALUES} -ovc xvid -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=${VBR}:pass=1 -oac copy -o /dev/null mencoder dvd://1 -dvd-device “$bn” -vf crop=${CROP_VALUES} -ovc xvid -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=${VBR}:pass=2 -alang en -oac mp3lame -lameopts br=${ABR}:cbr:vol=0 -o “\${NameNoExt}.avi” done
Basically, if you want better quality, then increase the video bitrate. However, this means the final output file will be bigger. Double passing also makes better quality output because the second pass uses information from the first pass to increase the bitrate at the part of the video that is needed. Good references are here and here. For me, I think setting the video bitrate to 1000 produces a pretty good quality video for the size. If you have a constraint on the file size (such as 690mb to fit on a CD), you can find a calculator here and here. simplerip is pretty cool in that it will create all the commands you need after you input your requirements (such as crop, file size, scale, volume, etc). However, I think my dvdiso2xvid_2pass.sh is a pretty decent script that automates everything and gives good quality output.