Saturday, June 18, 2011

How to convert .m2ts videos to .avi, using a script

The problem:
I have a bunch of vacation videos located in: /mnt/drive2/vacation_videos/ , but they are all .m2ts format, and I want them to be in .avi format

The solution:
- Create a generic script "convert.sh" which converts a .m2ts file to .avi
- Create a script "vacationVideos.sh" with all the custom vacation videos, and call the convert.sh script
- Run this as a background process, which allows you to log out of the session without interrupting the process. i.e. "nohup shellscriptname &"


1. Generic conversion script. Save as: nano /usr/sbin/convert.sh

#!/bin/bash
# AlanLupsha: this script converts .m2ts files to .avi files
# If you do not have mencoder, run sudo apt-get install mencoder
if [ -z $1 ] ; then
        echo "syntax: convert.sh file.m2ts"
        RETURNCODE=1
        exit $RETURNCODE
fi
mencoder "$1" -ofps 23.976 -ovc lavc -oac copy -o  "$1.avi"

2. custom script with the names of all my vacation videos. Save as: nano ~/vacationVideos.sh

#!/bin/bash
# This "vacationVideos.sh" script copies my vacation videos into a
# temporary directory and then invokes the convert.sh
# script which converts .m2ts format to the .avi format

MAINPATH=/mnt/drive2/vacation_videos/
TMP=/dev/shm
cd "$TMP"

FILE="2011.grandparents.m2ts"
cp "$MAINPATH/$FILE" "$TMP"
# convert the file from .m2ts to .avi (takes a long time)
convert.sh "$FILE"
# copy the converted .avi file back to the drive
mv "$TMP/$FILE.avi" "$MAINPATH"
# remove the temporary copy of the .m2ts file
rm "$TMP/$FILE"


FILE="2010.SouthFlorida.m2ts"
cp "$MAINPATH/$FILE" "$TMP"
# convert the file from .m2ts to .avi (takes a long time)
convert.sh "$FILE"
# copy the converted .avi file back to the drive
mv "$TMP/$FILE.avi" "$MAINPATH"
# remove the temporary copy of the .m2ts file
rm "$TMP/$FILE"


FILE="2006.DriveAcrossTheUSA.m2ts"
cp "$MAINPATH/$FILE" "$TMP"
# convert the file from .m2ts to .avi (takes a long time)
convert.sh "$FILE"
# copy the converted .avi file back to the drive
mv "$TMP/$FILE.avi" "$MAINPATH"
# remove the temporary copy of the .m2ts file
rm "$TMP/$FILE"

echo "Done converting!"

3. How to run:

ssh myname@linuxserver
nohup ~/vacationVideos.sh &

To see the progress:
tail -f nohup.out

To see the process:
ps aux | grep mencoder

To kill the process:
killall mencoder

Enjoy.

No comments:

Post a Comment