ffmpeg script to convert files - Programming On Unix

Users browsing this thread: 1 Guest(s)
pfr
Nixers
Hey nixers, I'm trying to figure out how to write a short script to convert all the media files in a directory and delete the old files.

I found this on the web looking for answers

PHP Code:
#!/bin/bash
for i in *.mkv; do
    
ffmpeg -"$i-codec copy "${i%.*}.mp4"
done 

So, my question is, how do I add other file types to the fist line so that if I have a folder which contains .mkv & .webm files then both file types will be converted to .mp4.

Lastly, as this will create newly converted files, how can I have all old files deleted within this same script?
_____________________________________________________________________________________________
“Maybe you have some bird ideas... Maybe that's the best you can do.” - Terry A. Davis (R.I.P Terry & Percival)
movq
Long time nixers
You just add the other types to the `for` loop. :) And for removing, well, use `rm`.

Code:
#!/bin/bash
for i in *.mkv *.webm; do
    ffmpeg -i "$i" -codec copy "${i%.*}.mp4"
    rm "$i"
done

Stuff after the `in` in a `for` really is just a list. You could also do this:
Code:
for i in hello world, how are you today?
do
    echo "$i"
done

It’ll loop over the individual words. Globs like `*.webm` will be expanded before the iteration begins.
s0kx
Members
Hi Ramiferous! I haven't actually played with ffmpeg before, but I'll assume that the command you provided is correct. This would be my solution to your problem:

Code:
#!/bin/bash
for i in *.{mkv,webm}; do
    ffmpeg -i "$i" -codec copy "${i%.*}.mp4"
    rm "$i"
done

You could also take out the rm command and run
Code:
rm *.{mkv,webm}
manually afterwards so you can first make sure that files were actually converted how you wanted. Hopefully this works for you :)
movq
Long time nixers
(15-03-2021, 03:00 AM)s0kx Wrote: I haven't actually played with ffmpeg before, but I'll assume that the command you provided is correct.
Now that you mention it, I’m not sure if that’s really what he wants. It will only change the video’s container format but keep the codecs. So you can end up with an “unusual” combination like ”VP9 inside of MP4”. Depending on which program is used as a player, this might break playback. (Something like `mpv` will probably almost always work.)
pfr
Nixers
(15-03-2021, 03:15 AM)movq Wrote:
(15-03-2021, 03:00 AM)s0kx Wrote: I haven't actually played with ffmpeg before, but I'll assume that the command you provided is correct.
Now that you mention it, I’m not sure if that’s really what he wants. It will only change the video’s container format but keep the codecs. So you can end up with an “unusual” combination like ”VP9 inside of MP4”. Depending on which program is used as a player, this might break playback. (Something like `mpv` will probably almost always work.)

Yeah, pretty much always using mpv (sometimes vlc if casting to chromecast).

So are the curly brackets necessary? seems to work without them.
ckester
Members
(15-03-2021, 07:57 PM)Ramiferous Wrote: So are the curly brackets necessary? seems to work without them.

Not sure if this is what you're asking about, but:

Code:
for i in *.mkv *.webm

and

Code:
for i in *.{mkv,webm}

generate the same list of files, although the syntax used to describe the pattern to be matched is slightly different. The bracketed notation is just a compact way to say either file extension will do.

It's a handy way to avoid retyping the common elements of two or more arguments. E.g.,

Code:
cp original{,.bak}

expands to

Code:
cp original original.bak

Apologies if I've misunderstood your question and explained something you already knew!
pfr
Nixers
(16-03-2021, 01:41 AM)ckester Wrote: Apologies if I've misunderstood your question and explained something you already knew!

That is exactly what I was wondering. As long as the script converts any file with those extensions to .mp4 I'm not fussed with the syntax. It is good to know though.