Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

How do I turn a command line into a shell script?

I've been experimenting with mencoder to convert my AVI movies into a format that I can download onto my cellphone and finally found a site where they explain the various parameters needed for the mencoder program to get it to work. Problem is, they're a pain to type in each time. How do I turn an ugly command line invocation into a nice shell script?


Dave's Answer:

Through an eerie coincidence, I too have been going through that same task, converting movies to dump onto my new Blackberry Pearl, which actually does quite a good job with video, rather to my surprise! It's not much bigger than a postage stamp, but it's watchable, and heck, it's a cellphone... :-)

Anyway, the information I found on using mencoder is from the8thsign.com. If you're on a PC, then you might find this posting in Pinstack.com's Blackberry forums useful.

The command I used is:

mencoder -vf scale=240:135 input -o output -of avi -ofps 15 -ovc \
lavc -oac lavc -lavcopts vcodec=mpeg4:vbitrate=230:acodec=mp3:abitrate=64

Scary, isn't it? No wonder you're looking for a way to avoid typing that in each time you want to convert a movie!

Fortunately, it's a very regular pattern and that means that it's not hard to replace "input" and "output" with specific variables and just prompt for those values when the script is started. That's exactly what the following script does:

#!/bin/sh

# This script converts regular AVI into the format needed for the
# Blackberry Pearl ...

mencoder=/Users/taylor/bin/mencoder

echo -n "input file: "
read input

echo -n "output file: "
read output

echo "Converting $input to $output..."

$mencoder -vf scale=240:135 "$input" -o "$output" -of avi -ofps 15 -ovc \
lavc -oac lavc -lavcopts vcodec=mpeg4:vbitrate=230:acodec=mp3:abitrate=64

exit 0

Save this as, say, "convert-movies", make it an executable with the command chmod +x convert-movies, and now you can just type "convert-movies" to get the script running:

$ ./convert-movies
input file: ../Movies/Bourne-Identity.avi
output file: ~/Desktop/bourne-identity.avi
Converting ../Movies/Bourne-Identity.avi to ~/Desktop/bourne-identity.avi...
MEncoder dev-CVS-051126-18:35-3.3 (C) 2000-2005 MPlayer Team
Mac OSX static build for ffmpegX
AltiVec found
CPU: PowerPC
. . .

Nice, simple, and far easier to remember. Note that I already knew the name of the input file, so you can't use filename completion as you could on the command line (in fact, it would be pretty easy to add the ability to specify the input and/or output filenames on the command line itself, allowing you to type in "convert-movie bourne-identity.avi bourne-identity-out.avi" or similar. I'll leave that as an exercise for you, dear reader, however!)



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Hi,

alternatively tou can use mencoder.conf file. It is a file where you can store your options and what is nice about it is that you can set some profiles there. This file should be found in ~/.mplayer/mencoder.conf if it's not you can create it.

So your file could look like this :

[bb] # name of the profile
vf=scale=240:135
of=avi
ofps=15
ovc=lavc=1
oac=lavc=1
lavcopts=vcodec=mpeg4:vbitrate=230:acodec=mp3:abitrate=64

You can also set default output filename:

o=output.avi

You can have many profiles like one above, you can set default settings using [default] profile.
To use a profile run mencoder like this:

mencoder -profile bb -o

You could make an alias of it or wrap a little script around it, which would for example save output file with the same name as input file but with ex. .avi.new extension.

So it would take only to type:

yourscriptname

to convert your file. And 'yourscriptname' can be shorter of course :)

With a script like that you can even make some batch conversion:

for i in *.avi ; do yourscriptname $i ; done

Those are just examples and you should tweak them to fit your needs

jot

Posted by: jot at March 4, 2007 6:39 PM

hello dave

we all very thankfull to you to update us in such usefully ways

with regard

chetan

Posted by: chetanM at December 29, 2007 6:11 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]