Score:1

Converting multiple WebMs to MP4

in flag

So, I've been researching how to convert all WebM videos to MP4 in a directory. I've looked for about half an hour for results, but unfortunately, Google isn't being my best friend. I found a simple command using ffmpeg to convert a WebM to MP4 in the terminal:

ffmpeg -i video.webm video.mp4

This is useful, but I don't want to do this several times when I have 30+ of these in the same directory. Is there a way to do all of it easy with a script?

ChanganAuto avatar
us flag
Perhaps using Handbrake is easier. You can queue all the files and then just wait for the results.
pasman pasmański avatar
mx flag
If you like `ffmpeg`, try `winff`.
Achak Claw avatar
in flag
So I can install it with sudo apt install handbrake? Or do I need to download it somewhere?
FedKad avatar
cn flag
Why don't you loop through all the `*.webm` files using a Bash script?
Achak Claw avatar
in flag
I don't know how to do that, which is why I'm asking :)
Score:4
om flag

You can do this using a shellscript:

for fname in *webm
  do
   ffmpeg -i $fname $(echo $fname | sed "s/webm/mp4/")
done

for fname in *webm is a for loop, where the elements that is iterated over is expanded from *webm, which will match all files ending in .webm

ffmpeg -i $fname $(echo $fname | sed "s/webm/mp4/") runs the command for each of the fname's that we aquired for the loop. $fname will expand to the current name. $(echo $fname | sed "s/webm/mp4/") uses the stream editor to rewrite webm to mp4, thus providing the correct filename for the output for ffmpeg.

FedKad avatar
cn flag
Please, correct the missing quotes for the two file name parameters of the `ffmpeg` command. Otherwise, the user will have problems with the file names that contain space characters.
Achak Claw avatar
in flag
So the MP4s will remain in this same directory right?
vidarlo avatar
om flag
Yes, it doesn't remove them. If you prepend `ffmpeg` with `echo` you can see what will be done step by step.
andrew.46 avatar
in flag
@vidarlo Perhaps this would be neater? `for f in *.webm do ffmpeg -i "$f" "${f%.webm}.mp4" done`
vidarlo avatar
om flag
@andrew.46 absolutely. Thanks!
udippel avatar
cn flag
FedKad is correct, of course. Why not just edit your code, and make that line look better?: `ffmpeg -i "$fname" "$(echo $fname | sed "s/webm/mp4/")"`
vidarlo avatar
om flag
@udippel Feel free to edit :) That improvement is well within what anyone can edit; it improves without changing intent :)
udippel avatar
cn flag
It's me, probably, but I don't like very much when others fiddle with my lines; and I wasn't the first one to point out the problem of filenames including spaces. I for one also prefer your version; for a 'askubuntu' your explanation is great. The proposal of andrew.46 might be shorter, neater; though also somewhat more obfuscated.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.