How to use FFMPEG to convert video via Ruby on Rails
Published: June 27th, 2007Use FFMPEG to convert videos in your Ruby on Rails applications, and even take screenshots.
So lately I took it upon myself to play around with some video transcoding.
If you don’t already have ffmpeg, you’re going to have to get the source somewhere.
I do it through subversion in my /usr/local/src directory (where I put all the stuff I want to compile)
svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg
Then you’re going to need to configure and compile it. Unfortunately it isn’t THAT simple, reason being that some libraries are needed to have audio in your conversions. Specifically, you have to download the LAME MP3 library, and anything else that your video’s may need, or codecs that you do require.
Since I basically only require the LAME library. Go ahead and download it from the sourceforge page.
untar it:
tar zxvf lame-3.xxx_.tar.gz cd lame-3.xx ./configure make sudo make install
You should now have the LAME mp3 library, you’re good to go on compiling FFMPEG w/ LAME support.
There’s actually a really simple way to get a good build of FFMPEG, and that’s to use darwinports. If you do have darwinports you can do the following and be good to go:
sudo port install ffmpeg
In fact, I’d seriously recommend using Darwin Ports for installing FFMPEG, as it does a lot of the pre-requieste libraries (like mp3 lame) for you, but if you can’t, then go ahead and continue compiling FFMPEG by doing the following:
svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg cd ffmpeg ./configure --enable-libmp3lame --enable-static --disable-vhook make sudo make install
You should now have a functioning FFMPEG program. Try this in your console to verify it:
sparta@phalanx mojobabyt $ ffmpeg --version FFmpeg version SVN-r9374, Copyright (c) 2000-2007 Fabrice Bellard, et al. configuration: --enable-libmp3lame --enable-shared --enable-encoder=libavcodec libavutil version: 49.4.0 libavcodec version: 51.40.4 libavformat version: 51.12.1 built on Jun 20 2007 14:44:55, gcc: 4.0.1 (Apple Computer, Inc. build 5367) ffmpeg: missing argument for option '--version'
Now, I am not going to get into specific tutorial-like examples of how you should set up your model to handle videos, or file uploading, or anything like that. What I will tell you will work, if you have a somewhat basic understanding of Rails, and Programming in general.
Basically, what you’re going to need is some way to upload a file, then in your model do something like this:
Create an after_save in your model, I did something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | def after_save logger.debug "testing the status of the current file #{self.filename} #{self.content_type} #{self.public_filename} #{self.full_filename}" if movie? convert_video_to_flv grab_screenshot_from_video end end def movie? if self.content_type == "image/jpeg" false else true end end private def convert_video_to_flv logger.debug "currently trying to convert the video ( #{self.filename} ) path ( #{self.full_filename}) from content type of #{self.content_type} to flv" # system "ffmpeg -i #{full_filename} -ar 22050 -ab 32 -y -f flv -s 320x240 #{full_filename}.flv" system "ffmpeg -i #{full_filename} -r 25 -acodec mp3 -ar 22050 -y -s 320x240 #{full_filename}.flv" # system "flvtool2 -U #{full_filename}" end def grab_screenshot_from_video logger.debug "Trying to grab a screenshot from #{self.full_filename}" system "ffmpeg -i #{full_filename}.flv -s 320x240 -vframes 1 -f image2 -an #{full_filename}.jpg" end |
Now that’s a good snippet that you can use to convert a video file, into an flv, and then it also grabs a screen-shot that you can use as a thumbnail, or whatever fancies your desires.
I hope this helps, I just wanted to share how simple it was, without getting to detailed. Let me know if you have some specific questions and I may take the time to help you out.

Check out the open3 lib if you want to run those commands and get access to stderr in case you have funky errors..
http://ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html
Hi. I added FLV converting to my site too, but I did it somehow different. I have a Ruby-Daemon-Script running, that does the converting. This has the advantage that none of the Mongrels running locks up while ffmpeg is running. But for small videos your approach seems fine.
Also thanks for the Screenshot snippet, I couldn’t figure out how to do it.
Yeah, that’s a very good point. I would definitely use a daemon, or “worker” of some sort if it’s for some serious production use. It really isn’t too hard to setup, and unless you’re doing really small video’s, the Ruby process is going to get locked up, and in that case the “worker” is really going to save your hide on that one.
[...] How to use FFMPEG to convert video via Ruby on Rails - Working with video? Really? So lately I took it upon myself to play around with some video trans-coding. [...]
[...] This looks promising : http://www.danielfischer.com/2007/06/27/how-to-use-ffmpeg-to-convert-video-via-ruby-on-rails/ [...]
[...] This page seems to spell out how to do that - there’s a lot involved. Here goes. [...]
hey…thansk a lot for u r post…
i used ur idea to integrate mencoder into rails and its works really gr8..!…
http://www.jroller.com/abstractScope/entry/flash_mp3_imageslideshow_media_player
this helps in flv player integration….
i use ffmpeg to grab_screenshot_from_video
the screenshot is ok after the server return the 500.rhtml page
can you help me
thanks
good example , how to generate snapshots of video
[...] http://www.danielfischer.com/2007/06/27/how-to-use-ffmpeg-to-convert-video-via-ruby-on-rails/ [...]
Hi, I have code for video upload similar to your example using ffmpeg. But I need to setup a worker to run in background, to upload videos more than 2-3 MB. I work on Windows. Can you please suggest a good site. Thank you
Great tutorial! Very clear and easy to follow.
I’m running into strange problems which I don’t understand, and I would like to enlist your help. I have had ffmpeg installed on a remote server, and everything seems to be working great. When I’m SSHed into the server, I can convert all types of videos easily (the command ‘ffmped -i input_video.type output.flv’ works great for all types, including both MOV and MPG). But when I run the code in RoR (using ’system “ffmpeg …”‘), I get success when using the same MPG files, but fail with any MOV files.
Any ideas as to what could be causing this? Thanks for any help!
Ready. Set. Go.
In terms of the formatting, you're allowed to use markdown, textile, or basic html; it's truly up to you -- what strikes your fancy?
You don't have to worry about your e-mail address being sold to a russian-spam-mafia. I'm only going to use it for my own weird needs; like asking you out for a date on a lonely night of coding.