Wednesday, March 25, 2009

COMMAND AND CONQUER

Ever had a video clip you
wanted to reduce in size
and add to a slideshow for a
presentation? Or, maybe convert
into another format so that you
could play it? Today, I will show
you how to do both of those
things using the command-line
tool 'ffmpeg'. I will also introduce
you to the 'mogrify' command,
which is an image-editing tool
contained in the imagemagick
package. It allows you to do many
things, but I will cover the basics –
mostly the resizing of images (e.g.
for thumbnails or other small
images).
To use these tools, you will need
to install ffmpeg and
imagemagick through either
Synaptic Package Manager or
Add/Remove Applications, or, in
the spirit of this article, through
the command-line with:

sudo apt-get install ffmpeg
imagemagick

It's safe to run the install
command – if you're unsure whether
or not you've already installed it –
since apt-get will not overwrite the
existing program, but merely inform
you that it's already installed. Also, it
will ask you for your password (since
you're using sudo), and, if you're new
to this, you may be surprised that it
does not show anything when you
enter your password. This is normal,
just type your password and hit the
enter key.
For this article I will be converting
a short clip from "Freedom
Downtime" which I used in a
presentation. ffmpeg offers a lot of
options (which you can read about in
detail in the roughly 13-page-long
manpage – by using the command
"man ffmpeg"), but the option I use
most frequently is the option to
convert files. The format for such a
command is:

ffmpeg -i inputfile.filetype
outputfile.filetype

That command would just convert
the input file to whatever output file
you specified in 'output.filetype'
– without changing the size
(since, if the size is not specified,
ffmpeg defaults to the size of the
source). However, to convert
"Freedom Downtime" (starting
size of 640x480) to a smaller clip
(say 320x240), the command
would look like this:

ffmpeg -i freedom\
downtime.mpg -s 320x240
freedom\ downtime\ resized.mpg

Of course, you could also
change the format of the file at
the same time by changing the
file extension. This shouldn't
take too long (obviously
depending on the size of the
clip). My clip was a few minutes
long, and took maybe 30
seconds to convert on my laptop.
Mogrify is a very handy tool,
especially if you find yourself
posting a lot of pictures on web
forums that don't allow uploads
of images exceeding a certain
size, or do not allow linking to
large images for previews in
posts. I use mogrify mainly for
making thumbnails of images,
but it can do many more things,
such as adding text, adding
effects (charcoal, colorized, etc.)
and much more (it's all outlined,
once again, in the manpage,
reached with "man mogrify").
Mogrify supports resize
arguments in either percentages
or pixel values. So, for example, if
you have a 1280x800 pixel image
which you want to resize to
640x400, you can do so with
either:

mogrify -resize 50 in.jpg
out.jpg

or:

mogrify -resize 640x400 in.jpg
out.jpg

or even just:

mogrify -resize 50% in.jpg
out.jpg

However, if the pixel
measurements and the aspectratio
you supply are different, it
could result in a smaller image
than expected, as it will scale to
the nearest values that are still in
proportion. Mogrify also has a
thumbnail argument, which does
roughly the same as the resize
option, yet it removes any
unnecessary comments, etc. from
the file header to reduce image size.
So using that tool the command
would be:

mogrify -thumbnail 50 in.jpg
out.jpg

You can also use it to convert
images using the “-format”
argument. So,

mogrify -format jpg *.png

will convert all the .png files in the
current directory into .jpg format
(names will be the same).
As you can see, contrary to
common belief, command-line tools
can help with graphical projects, and
often do it more quickly or more
effectively than a GUI program with
confusing menus and/or different
layouts in newer versions. The
command will (usually) stay the
same, and arguments are very rarely
changed. And so, command-line tools
are much more universal – which is
why users on ubuntuforums.org
usually supply commands instead of
GUI methods for solutions, since the
commands apply to Kubuntu,
Xubuntu and Ubuntu, as well as
you've found this useful, and
next time you need to convert a
video or an image, you'll
remember mogrify and ffmpeg.
After all, practice makes perfect.

From Full Circle

No comments:

Post a Comment