Given an AVI encapsulated video with WMA2 audio and MP4 video how do I pass-through the video, convert the audio to MP3; then re-encapsulate the entire file into an AVI?

ffmpeg -i "foo bar.avi" -acodec libmp3lame "Converted\foo bar.avi"

Now how do I do that same command for every .avi file in this directory?

(need a solution using bash or Windows CLI [for?])

  • Are you observing any particular failure on running the command given in the question?
    – anishsane
    Dec 4, 2012 at 12:21
  • Nope; I am not.
    – A T
    Dec 4, 2012 at 12:25
  • So this command is successful & you see the output file generated as you want?
    – anishsane
    Dec 4, 2012 at 12:32
  • 1
    By the way, if you want to pass through video "AS-IS", then add -vcodec copy in the ffmpeg command line. It will save video conversion time + retain the same quality in video.
    – anishsane
    Dec 4, 2012 at 12:33

4 Answers 4


Do this not simply work?

for file in *.avi;do ffmpeg -i "$file" -acodec libmp3lame "Converted\\$file";done


There seem to by a backslash problem...

Try this:

for file in *.avi;do ffmpeg -i "$file" -acodec libmp3lame 'Converted\\'"$file";done

or this:

for file in *.avi;do ffmpeg -i "$file" -acodec libmp3lame 'Converted\'"$file";done
  • @imp25 Maybe do you need to escape the backslash: ` \\ ` instead of ` \ `
    – F. Hauri
    Dec 4, 2012 at 12:20
  • Nope, now getting same error (the final error I mentioned) on both // and `\\`
    – A T
    Dec 4, 2012 at 12:24
  • Well, there is a conflict as Windows use ` \ ` for path and shell use ` \ ` for escape. Unfortunely ` \$var ` could be used to print effectively $var. So try the editer version, maybe.
    – F. Hauri
    Dec 4, 2012 at 12:47
  • The first version should work in Bash.
    – slhck
    Dec 4, 2012 at 13:37

In bash the following will do what you want if your run it in the directory which has the .avi files:

for file in *.avi; do
    ffmpeg -i "$file" -c:a libmp3lame "Converted\\$file";
  • I was first ;-)
    – F. Hauri
    Dec 4, 2012 at 11:45
  • Thanks, but neither worked. I am using Cygwin bash if that makes a difference.
    – A T
    Dec 4, 2012 at 12:12
  • Was there any error message? How did it not work?
    – imp25
    Dec 4, 2012 at 12:18
  • [NULL @ 03890340] Unable to find a suitable output format for 'Converted$file' Converted$file: Invalid argument then after changing \` to /` got this error: Converted/my filename here.avi: No such file or directory. It was able to open the file (stream info for the input is above this text).
    – A T
    Dec 4, 2012 at 12:22
  • What happens when you use a full path, and using cygwin's path format (which follows unix's /path/to/foo format)? You can convert a windows path into a cygwin path using cygpath; see stackoverflow.com/a/4337071/1608708.
    – imp25
    Dec 4, 2012 at 12:51

Python solution

from sh import ffmpeg
from os import listdir
from os.path import split
from sys import argv

[ffmpeg("-i '{0}' -acodec libmp3lame 'Converted\{0}'".format(_file))
 for _file in sum([listdir(split(arg)[0]) for arg in argv], [])]

After a trial & error conversation with the commands involved I found that this works in a bash shell

for i in *.ts; do ffmpeg -i "$i" -threads 6 -f mpegts -vcodec libx264 -preset slow -bufsize 8000 -async 48000 -acodec ac3 -ab 384k -n subdir/"${i%}"; done

this makes a high quality compressed version of input .ts files with the same name but in a subdir. I'm guessing it wont write a subdir if it doesn't exist so you would have to specify one that does in substitution for what I'm calling "subdir". Also I used -n to keep it from overwriting files I individually rendered before I discovered the proper syntax for this batch process.(I have a 6 core processor so "-threads" should be adjusted as well to your hardware)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.