Tuesday, June 14, 2005

Cutting and Joining video clips through mencoder ( mplayer)

to cut, use

mencoder uncut.avi -ss 0:20 -endpos 10 -ovc copy -oac copy -o cut.avi

-ss is for starting position and -endpos is for ending position. Use man pages for more detail.

For joining, I have got this script from somewhere, I never tried it myself though :-)

#!/bin/bash
#
# $Id: join-avi-files,v 1.3 2004/09/26 16:10:36 thor Exp $
# Tom Moertel
#
# Usage: join-avi-files outfile.avi in1.avi in2.avi ...
#
# This program joins the given movie files in1.avi, in2.avi, ...
# and saves the result as outfile.avi. Although the name is
# join-avi-files, this program will join any kind of movies,
# provided that mplayer knows about them and that the movies
# all use the same codecs, resolution, stream rate, etc.

# COPYRIGHT AND LICENSE
#
# Copyright (C) 2004 Thomas G. Moertel.
# All rights reserved worldwide.
#
# This code is licensed under the GNU General Public License, version
# 2 or greater. It is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.


# CODE

# preflight inputs

if [ $# -lt 2 ]; then
echo Usage: $(basename $0) outfile.avi in1.avi in2.avi ... 1>&2
exit 1;
fi

# shift the output file from the command line

outfile="$1"
shift

# as a safety precaution, make sure the output file doesn't already exist

[ -f "$outfile" ] && {
echo "$outfile already exists; aborted." 1>&2;
exit 2;
}

# create a temporary directory in which to do our work

tmpdir=$(mktemp -d join-avi-files-XXXXXXXXXX)
[ -d "$tmpdir" ] || { echo "can't make tmp dir" 1>&2; exit 3; }
mkdir "$tmpdir/in" || { echo "can't make in dir" 1>&2; exit 3; }
mkdir "$tmpdir/out" || { echo "can't make out dir" 1>&2; exit 3; }

# make sure the temporary directory is cleaned up

cleanup() { rm -rf "$tmpdir"; }
trap cleanup 1 2 3 5 13 15

# massage each input file into valid form

counter=100000
for infile in "$@"; do
echo
echo === SCANNING INPUT: $infile ===
echo
base=$(basename "$infile")
mencoder -idx "$infile" -ovc copy -oac copy -o "$tmpdir/in/$counter-$base"
counter=$(( $counter + 1 ))
done

# merge the massaged input files

echo
echo === WRITING OUTPUT: $outfile ===
echo

outbase=$(basename "$outfile")
cat "$tmpdir/in/"* > "$tmpdir/$outbase"
mencoder -noidx -ovc copy -oac copy -o "$outfile" "$tmpdir/$outbase"

# clean up and exit with success

cleanup
echo
echo Done!

exit 0

No comments: