Showing posts with label Media. Show all posts
Showing posts with label Media. Show all posts

Sunday, January 10, 2010

Introduction to FFMpeg

FFMpeg is an open-source utility for recording,streaming and converting digital video and audio. We will see the usage of a few functionalities to convert between multiple file formats.

In simplest form ffmpeg can be used as:

ffmpeg -i <input filename> <outputfilename>

Eg: Convert a flash (flv)video to avi format

safeer@penguinpower:~$ffmpeg -i SELinux4EveryOne.flv SELinux4EveryOne.avi

This will throw a lot of output and then start converting the video. Once the conversion is finished play the the file with a media player like vlc or mplayer.

This should play the new avi file nicely (provided mplayer is installed):

safeer@penguinpower:~$mplayer SELinux4EveryOne.avi

Now, what if we want to extract the audio track alone from the Video? This is the command.

safeer@penguinpower:~$ffmpeg -i SELinux4EveryOne.flv SELinux4EveryOne.mp3

You can also convert flv to wav or wav to mp3

safeer@penguinpower:~$ffmpeg -i SELinux4EveryOne.flv SELinux4EveryOne.wav

safeer@penguinpower:~$ffmpeg -i SELinux4EveryOne.wav SELinux4EveryOne.mp3

A wide variety of media formats are supported by ffmpeg. To get a full list, use "ffmpeg -formats"

A few useful options:

-f <format> : Usually ffmpeg can determine the output format by the extension name itself. But incase you want to force the output format, use this option.

Eg: ffmpeg -i SELinux4EveryOne.wav -f mp3 SELinux4EveryOne.mp3

-ab : Audio bitrate ( 64k is default, 128k would be a reasonable value )
-ar : Audio frequency - 44100 Kz is default
-ac : Audio channel, a value of 1 is mono, 2 is sterio
-an : Disable audio recording.
-b : Video bit rate
-r : Video frame rate
-s : Video frame size ( Screen resolution )

Any option that you give before any input/output file will apply to that file. Also remember that you can have multiple input files each specified with "-i" option and multiple output files ins some cases.

A couple of other interesting uses of ffmpeg:

Extracting images from a video:

ffmpeg -i myvideo.avi -r 1 -s svga -f image2 img-%03d.jpg

This will extract one image per second from the video for its total length. The frame size of the images will be 800*600 ( svga ). The output file name img-%03d.jpg will be automatically expanded and used in the following way: the part "%03d" will change to 3 digit numbers padded with zeros - img-001.jpg,img-002.jpg....img-031.jpg ...

Video capturing yout desktop can be done with :

ffmpeg -f x11grab -s xga -r 50 -i :0.0 /tmp/out.avi

This will capture "1024x768" sized portion from the upper left corner of the screen. The frame rate will be 50 frames per second.

There many such uses for ffmpeg. to learn more, checkout their documentation.

Wednesday, January 6, 2010

Downloading Youtube videos in Linux

Lots of people who watch Youtube videos like to download them for viewing it offline later. On a Linux box, the best tool you can find is a small command line utility called "youtube-dl". This is available as package in fedora/debian/ubuntu/suse repositories. Even if this tool is not in your repository, you can download it directly from the project website. This script requires python, so make sure you have it. Unzip the downloaded archive and copy it to system path (or run from there itself - if you wish).

To download a video, you need to get its Youtube URL which is the URL that you see in the address bar of your browser when you play the video. It will look like this: http://www.youtube.com/watch?v=RANDOM_STRING. The RANDOM_STRING will contain alpha numeric characters and dash.

Let us see the actual use of the tool.

safeer@penguinpower:~$youtube-dl http://www.youtube.com/watch?v=QTw6iexyrNU

It will throw some output about the URL and then start downloading, showing the current download speed and ETA. When finished, it will report "Video data saved to QTw6iexyrNU.flv". The file will be saved in the current directory. If you want to save to another location or change the name of the file, use option "-o".

The default format for the saved video will be "flv" but if you use the -b option ( for best qulity ) the format will be mp4. So change your filename accordingly when using -o and -b together.

There are many other useful options including the option to give username and password for videos that can be accessed only by logged in Yuotube users. Use "youtube-dl -h" to see all the options.

One thing that you should remember about Youtube URLs is that if the url contains one or more "&" symbols, you need to take only the part on the left hand side of the first "&". For eg, if you got this URL "http://www.youtube.com/watch?v=QTw6iexyrNU&feature=related" from your browser, then the actual URL is "http://www.youtube.com/watch?v=QTw6iexyrNU". The rest are arguments to the Youtube website which will not be used by youtube-dl.

This tool has a windows GUI which can be found here.

Now for the people who want to do things their way, following method may be interesting. Open your browser and play the video from Youtube and wait for it to finish. Once the video is played to the full length, open a terminal keeping the Youtube page open. Then run this command on terminal.

safeer@penguinpower:~$ for i in $(find /tmp -type f -user $(whoami) 2>/dev/null);do [[ $(file $i) =~ "Flash" ]] && echo $i;done

/tmp/FlashRXaVqD

This file is the video you just watched, if you want to cross check you can run this file in some media player like vlc.

safeer@penguinpower:~$ vlc /tmp/FlashRXaVqD&

And it will play the video. Now save the file with a name and flv extension.

safeer@penguinpower:~$mv /tmp/FlashRXaVqD ~/chess-play.flv

Not a big deal, but it will save your bandwidth, if you want to first play the video and then only download it.

Chrome is developing a browser extension for Youtube downloading, I haven't tested it yet. Have a look into it if you are interested.

Disclaimer: Downloading Youtube content is subject to copy right and content policies of the concerned parties, the author or this blog will not be responsible for such issues.