Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a bunch of files which need to be converted from .mp4 to .mpeg.one. 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
3  
add comment

3 Answers 3

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
add comment

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
add comment
  • 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
add comment

Your Answer

 
discard

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.