Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bunch of files which need to be converted from .mp4 to A friend of mine had suggested me this simple script. However it doesn't work because filenames contain blank spaces.

Here is the script:

for f in $(ls *.mp4); do ffmpeg -i "$f".mp4 "$f".mprg; done
share|improve this question
up vote 10 down vote accepted

You don't need to use ls in this case. As a general rule, always try to use shell builtins and glob features instead of relying on external programs (e.g. ls) that can introduce problems with ugly workarounds.

Also, you shouldn't 'manually' append the extension to the input file name (the one you give to -i) and you should strip the extension on the output file.

So the command should be:

for f in *.mp4; do ffmpeg -i "$f" "${f%.mp4}".mprg; done
share|improve this answer
Note that ffmpeg chooses the output container format depending on the extension. If you want another extension, you have to specify the container format - in this case with -f mpeg. – phoibos Feb 3 '13 at 13:00

Instead of ls, you can use find -print0 and xargs -0:

find -name '*.mp4' -print0 | xargs -0 -n1 ffmpeg -i '{}' '{}'.mpeg

Followed by a rename afterwards:

rename s/mp4.mpeg$/mpeg/ *.mp4.mpeg
share|improve this answer
  • No ls needed
  • You probably mean .mpeg as extension
  • If you place the " right, spaces won't be aproblem
  • ffmpeg -i "$f".mp4 in your command will look for e.g. files.mp4.mp4, which you probably didn't mean
  • Be aware that your output files will be named e.g. files.mp4.mpeg

Possible solution:

for f in *.mp4; do ffmpeg -i "$f" "$f".mpeg; done
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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