How To Convert Any Video File Format Under Linux
This video tutorial will explain how to losslessly convert any video file format, including quicktime .mov, flash .flv files, open source .ogv, .mp4, .wmv, .asf and more. I show you how to install ffmpeg, check the formats and codecs available to you, convert a file to a new format (windows media and .asf in this example) without any loss in quality during the decoding and encoding process, and create and run a script file that will enable you to run a batch conversion on any number of files at the same time.
Anyone who has tried to convert multimedia files in Windows knows how challenging it can be. There are a ton of different programs out there to do it, but most of them either leave a watermark in your video, lower the resolution of your video, or both, unless you pay hundreds of dollars. ffmpeg can do this for you quickly, conveniently, and completely free – and can even be used in Windows! This video was created for Ubuntu, but will work for most Linux distributions, and the code you learn here to convert a file can be applied to the Windows version as well, though Windows’ scripting language is different so you will need to adjust the batch conversion script.
Follow me on Twitter and check out my blog.
Single File Conversion
sudo apt-get install ffmpeg
ffmpeg -formats (lists all available formats based on the currently installed codecs, to be used with -vcodec, -acodec, and -f)
ffmpeg -i inputfilename.ext -vcodec wmv2 -sameq -acodec wmav2 -f asf outfile.asf
ffmpeg – start ffmpeg
(-i inputfilename.ext – tells ffmpeg the file name and extension from which to convert)
(-vcodec wmv2 – use video codec wmv2)
(-sameq – use same video quality as source file)
(-acodec wmav2 – use audio codec wmav2)
(-f asf – choose output format, sometimes known as a container, in this case asf)
Bonus Batch Conversion
sudo touch convert.sh (here we use touch to create a blank file, not always necessary to do that rather than through a text editor but it is safer with permissions and such)
gksu gedit convert.sh (this is only available for gnome desktops, i.e. ubuntu and the like, so you can tell people to use whatever text editor they like)
sudo chmod 0777 convert.sh (like last time, chmod changes file or directory permissions, in this case giving access to everyone to read, write, execute the script)
./convert.sh (runs the script convert.sh on files in the /Videos directory)
ls (ls -l lists all files and directories plus permissions)
script:
for f in *.MOV; do ffmpeg -i “$f” -vcodec wmv2 -sameq -acodec wmav2 “${f%.MOV}.asf”; done
If you liked this article, please share it on del.icio.us, StumbleUpon or Digg. I’d appreciate it.
iEntry 10th Anniversary
LinuxHaxor
WH
MH
Nice guide, but most new users always have a pathological fear of commandline, and when they say Linux is not ready for the desktop. what they actually mean is that we are not ready to learn the commandline no matter how easy it might seem. Fortunately There are tools that have been able to unlock almost every commandline utility (at least for desktop use) on linux hence the use of commandline solution should not be the first to be thrown towards a new user.
For your guide there is a tool called Winff which can be downloaded from http://www.winff.org that unlocks the powers of ffmpeg in a simple, easy to use gui interface. winff works like a charm and as video profiles for all ranges of iPods,Palm,and even android phones amongst others. I use it often and it has never failed me. I would prefer it over using ffmpeg from commandline any day.
Few tips:
When writing commands that should be typed in use $ as a prefix:
$ ffmpeg -formats
…
$ sudo chmod 0777 foo.sh
The comments and explanation that you enclosed with parentheses should be written as all comments in shell scripts – prefixed by #:
#
# This commad will make everything go away and delete all your files
#
$ rm *
When writing a shell scripts it is always wise to start them with
#!/bin/bash
in the first line. For example if user would be running some other shell instead of bash (tcsh, csh, …) your for() loop wouldn’t work since it works only in bash.
The convert.sh won’t work if someone will copy and paste it into file. Because command will span over two lines. It should be written like this:
for f in *.MOV; do ffmpeg -i “$f” -vcodec wmv2 -sameq -acodec wmav2 \
“${f%.MOV}.asf”; done
or (the one I prefer)
for f in *.MOV
do
ffmpeg -i “$f” -vcodec wmv2 -sameq -acodec wmav2 “${f%.MOV}.asf”;
done
Ok, now I’ll start sounding like a real pain in the ass… I guess someone has to? Or?
Anyway, you should enclose all the code in this site in <pre> tags. Right now your quotes are replaced with something that is not a quote in bash or any other shell script. Copy pasting will break the script and your commands. Is this blog running on WordPress? Try using some code highlighting plugins. I’m using WP-CodeBox on my blog. It is pretty. :)
One more last thing, 777 are bad permissions, bad bad bad. ;) 755 should be more than enough for simple video conversion.
Ok, now really a last thing. Touching the file before editing doesn’t really matter if you chmod it later.
That’s enough. I better stop now. :>
how can i convert my files to xvid codec using double pass filter?
Here is my collection of ffmpeg commands.
mm, I tried:
ffmpeg -i kk.mov -vcodec h264 -sameq -acodec acc -f m4v kk.m4v
and I get an error: “unkonwn encoder” although h264 seems to be in the list of encoders ….
And thanks for the video !!
Can this be used to convert video files to sound files….say mp4 to mp3? Thanks!
Wow, aint she a little cutie! Hey now!!
why do you use sudo to touch and edit your file? It’s more save to use gedit in user-mode. If you want the script to be owned by root and/or in /usr/local/bin, you can use chown and mv with sudo.
Also, like BigWhale point out, permissions shouldn’t be more then 755. 777 is very bad.
just go here and you will find the easy way to do any (almost) conversion in linux
http://www.gnome-look.org/content/show.php/Audio%2BVideo%2BImage%2BText%2BISO+Convert?content=92533
The introduction seems a little misleading; given that you’re dealing with lossy video compression codecs, I think it is by definition impossible to convert them losslessly. When going to or from a lossy-compressed format, there will always be some quality lost; it may be imperceptible, but the more times you repeat the process the more you’ll see it.
Sorry, but this is more than a cheap nitpick. What you are doing is more getting a quality in the same ballpark in a different format. But it’s not a lossless conversion at all.
For example, if you save a jpeg file over and over again with the same quality settings (viz. -sameq) then the image will degrade more and more. Because jpeg is not lossless and neither are most of video codecs.
Don’t bother to read, but this would be the wikipedia article: http://en.wikipedia.org/wiki/Lossless
Yeah, what he said.
I think I’m in love, hot and geek ;)
$Girl, I love you…
Muah!!!
Here is a nice GUI that has ffmpeg installed. Works great.
http://www.erightsoft.com/SUPER.html
Bookmarked for later reference! Thanks for the tip. Nice useful command line tool.
Sorry to bang with the same, but I am still wondering what of the doesn’t-mean-lossless meanings of lossless she is referring to?
Like loosing no hair because of lack of frustration? That might be it, yeah…
There’s a much easier way. Install Wine and install MediaCoder in Wine. It has all the necessary codecs internalized and works great.
You are awesome, however there is this little app called WinFF that makes this so much easier. Look it up in add/remove in ubuntu.
Thanks for the feedback – yes, the code could be cleaned up, and thanks for letting me know about winff. Yes, my language could be more precise about lossless. The conversion adds imperceptible loss, so I improperly called it lossless.
I may bash Windows, and it is fun to do so, but I still use it for gaming and other things, so don’t think I am some sort of cult follower. I just like to use the right tools for the task at hand, and ffmpeg does its job very well and very quickly, with no fuss. I haven’t tested the code in Windows but the same command line should work with the Windows version as well.
hombrelobo – are you sure you’re using the proper audio codec? It should be aac, not acc, right?
um. this is NOT lossless. this is LOSSY transcoding. i.e. LESS QUALITY than the original.
lossless transcoding with ffmpeg would use -vcodec copy and -acodec copy (or an uncompressed equivalent such as WAV, PCM, HUFFYUV, etc)
so… a totally useless thread. thanks.
I’m sorry that you felt it was worthless. I feel it was worthwhile to make, regardless of a particular mistake, and it seems to have helped some people. Unfortunately you can’t please everyone.
Handbrake works on Ubuntu. I haven’t got round to it yet but I hear it is like the VLC of video conversion and uses ffmpeg underneath
http://handbrake.fr/
what about real audio?
I appreciate you really sticking to the “right” way to do it in linux, providing the command line references for it, it’s always useful to have it in that format.
Thanks for the writeup, I’m following you on twitter :)
Just take out that lossless crap already. thanks. whatever gave you the impression any encoding to a compressing format could be lossless?
Thanks for the batch script, I’ll use it with the bulk save feature on http://www.vidimonkey.uni.cc to save some youtube dailymotion veoh videos.
@nixie yes, you are right, it should be aac … :D …. but still the same error, “Unknown encoder ‘h264′”
By the way, I am using Arista, personally I think it is far better than WinFF or handbrake: http://programmer-art.org/projects/arista-transcoder
And thanks for the videos !!!
Good work!
The ‘lossless’ issue is a minor one- don’t mind the naysayers. Numerous people will appreciate this video greatly. Most of all, you’re helping to make the command line accessible with a well produced and informative video. One of the best I’ve seen yet.
Keep up the good work!
(/me fwd’s the video liberally)
avidemux works pretty good, i’m a newbie and its GUI, so its all good. I like the command line as well, and I know its so powerful and quick but I don’t have time to read all that stuff! Oh yeah you’re hot, I agree…THIS is how you promote Linux :) in Other Operating Systems.http://forums.overclockers.com.au/showthread.php?t=781809
Of course, this only works on codecs that Linux can read. I recently came
across some old IV50 videos from ages ago when I ran Windows. There is no
Linux codec for this format, and the various workarounds that involve using
.dll files borrowed from 32-bit windows don’t work on a 64-bit Linux (which is
what I am running.)
Its looking like I’m going to have to go and ask someone with a windows box
to convert them to XVID for me.
Nixie, I am old enough to be your grand-dad, maybe your great-grand-dad. But, I am not dead, so I also think you are hot. However, since I am old, my definition of hot is different than the young guys. To me, smart is hot, and you sure seem smart-hot, heh, heh.
Physically hot is very temporary. Smart-hot lasts almost forever, unless you get dementia when you are 80. A smart 60 year old woman is still fun to be around. There is no such thing as a 60 year old ‘babe’.
Don’t take any nonsense off guys who only think you are physically hot. Hang around guys who think you are smart-hot. Trust me on this. You are not the first smart-hot woman I have known in my nearly 70 years.
You should be safe with Linux guys; most are smarter than average, too.
And, I agree, the lossless booboo is trivia, pure trivia.
I found this very interesting, thanks for posting it! Linux since December 1999. Currently using Kubuntu 6.10; 7.04; 7.10; 8.04; GOs; Mandriva; CentOs; and ready to install Kubuntu 9.04 tomorrow on my two computers. I multi-boot which gives me a lot of distros to play with. I have a large storage FAT 32 partition, and set up links to each distro as needed so everything important is available on all distros.
Other frontends for ffmpeg are hypervc (Hyper Video Converter) and shazam.
http://www.gtk-apps.org/content/show.php/Hyper+Video+Converter?content=88970
http://shazam.sourceforge.net/
Can we not get any more video tutorials? I was hoping this video thing was just a temp fad on LinuxHaxor, but it’s still here. :(
What is a “container”? What is asf? and what is outfile.asf? You did not explain these things in your video.
I tried to convert a file in the using ffmpeg four times. I kept getting error messages. The forth time is worked. Now let’s see if it plays.
By the way I have a core 2 duo. I noticed that while encoding one core is at 100% and the other is at 11%. Is something wrong?
Hi Nixie,
Thank you for these nice CLI tutorial , keep up the good work .
for GUI programs this another tool for converting video : MMC (MobileMediaConverter)
http://www.miksoft.net/mobileMediaConverterDown.htm
it’s uses ffmpeg for back-end .
Cheers,
Free-Programmer
The Hulk,
Your CPU usage means that ffmpeg is utilizing 100% of one core and the other core is used by different processes. This is normal, since ffmpeg is not multi-threaded application and doesn’t know how to use more than one core.
What a complete bull, you could download ffmpeg or any other FOSS on a Windows platform that has the same functionality with a GUI front end, to convert your video files. Nice touch on the FUD side of things, next time try viewing Flash or DVD or Blue ray on Linux without having Video Tear. A GRAINY conversion on Video on a Windows platform, ha! That’s a lot of laugh.
Your presentation is pretty nice.
Now, could you do a presentation to refute this:
http://www.montanalinux.org/lfnw-2009-lunduke.html
It’ll be nice if you can refute this too:
http://www.krsaborio.net/research/2000s/09/0128.htm
Thank you.
ok so you have downloaded all the sections to a movie and want to join them back together……what is the best program to do this?
You can try Cucusoft DVD and Video Converters Suite which includes Cucusoft Ultimate DVD and Video Converter Suite. It is the best all-in-one DVD and video conversion software converts DVDs and videos to play on almost any portable device including iPod, iPhone, Zune, PSP, video capable MP3 players, video capable mobile phones, and Pocket P.C., etc.
There is one MAJOR problem and one minor problem with the scripts that people are people are using to convert the files.
“${f%.MOV}.asf”
The above code snippet does one thing; it renames the file from:
.MOV to .asf
The problem is that their is no check to see if there is an existing file
name. For example you’re converting. “movie.MOV” and run it through
the script. The result should be “movie.asf”.
SUMMARY:
“movie.MOV” to “movie.asf”
There is no check to see if “movie.asf” exists. If it does exist; then the
file will be overwritten. You could loose a file or wind up reconverting
the same file.
The minor problem is that script assumes that there are files with spaces
in their names. Not actually a bad thing; but it really should be
converted to something else: (IE: underscrore, period, something).
Just as long as people understand that they may clobber / overwrite a file
then tis ok.
Just a suggestion:
1) script to remove spaces and special characters from filenames
while checking to see that I don’t over write files when the get renamed.
2) Script the conversion program to make sure that the files to be
converted don’t over write existing files.
3) break out popcorn.
Just a heads up to people about the potential for having your files
overwritten.
Regards,
Pete
Nice Troll Hale Smith:
Nice touch on the FUD side of things, next time try viewing Flash or
DVD or Blue ray on Linux without having Video Tear.
A GRAINY conversion on Video on a Windows platform, ha! That’s a
lot of laugh.
1) Really I got Flash 10 running on Linux. Clueless +1
2) DVD there are codecs in the variousrepositories that allow you to watch
DVD’s providing that you’re not in a coutry that has some laws about
distributing certain codecs. If you are in such a country you can buy
your own codecs through: wwwfluendo.com/ Clueless +2
3) GRAINY conversion. Well WMP out of the box has poor support
for a lot of video codecs. You need to add tons to make WMP functional
such as Divx, mpeg4 part 10, etc. Unless you use a video player that
supports it. IE: VLC, mplayer, etc. You can risk downloading codecs
from various sites; but your only going to wind up using:
Norton an McAfee
Spybot
Winddwos Defender
Malwarebytes
and etc to fix you machine.
Regards Troll..
i can be helped with ffmpeg but i am using windows xp, i think it works the same.