0

I want to convert a bunch of mkvs to mp4s with the following command:

for i in $(ls *mkv); do ffmpeg -i $i -vcodec copy -acodec copy $i.mp4; done;

However, the filenames have whitespaces and hyphens in them, so the command does not work properly. (When I manually replace The File - 01.mkv with The\ File\ \-\ 01.mkv, the same command works).

What's the best way to get this to work?

Anthon
  • 78,313
  • 42
  • 165
  • 222
  • This is a classic [XY problem](http://meta.stackexchange.com/a/66378/271976). You wanted to do something with files with spaces in their names, but you guessed at a solution and then titled your question with a reference to your guessed solution. – G-Man Says 'Reinstate Monica' Mar 07 '15 at 00:57
  • I don't think any of the answers to that question actually answer this one, though. They're all about `find`. – Michael Homer Mar 07 '15 at 04:05

1 Answers1

8

You need to quote the variables and avoid the command substitution:

for i in ./*.mkv; do ffmpeg -i "${i}" -vcodec copy -acodec copy "${i}.mp4"; done

See When is double-quoting necessary? for a detailed explanation of quoting.

While I'm at it, the above produces files with a .mkv.mp4 extension; to fix that:

for i in ./*.mkv; do ffmpeg -i "${i}" -vcodec copy -acodec copy "${i%%.mkv}.mp4"; done
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • 2
    Or use `zsh` that doesn't have all those misfeatures: `for i (./*.mkv) ffmpeg -i $i -vcodec copy -acodec copy $i:r.mp4` – Stéphane Chazelas Mar 06 '15 at 15:06
  • @StéphaneChazelas - it's possible that neither method is foolproof. Apparently [`ffmpeg` interprets its own quotes](https://www.ffmpeg.org/ffmpeg-all.html#Quoting-and-escaping) - single quotes, backslashes, and double-quotes, though at a glance it appears it interprets double-quotes as the harder form. *Note that you may need to add a second level of escaping when using the command line or a script, which depends on the syntax of the adopted shell language.* – mikeserv Mar 06 '15 at 17:55