nixers
ffmpeg script to convert files - Printable Version
+- nixers (https://nixers.net)
+-- Forum: Development & Graphics (https://nixers.net/Forum-Development-Graphics)
+--- Forum: Programming On Unix (https://nixers.net/Forum-Programming-On-Unix)
+--- Thread: ffmpeg script to convert files (/Thread-ffmpeg-script-to-convert-files)


ffmpeg script to convert files - pfr - 15-03-2021

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?


RE: ffmpeg script to convert files - movq - 15-03-2021

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.


RE: ffmpeg script to convert files - s0kx - 15-03-2021

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 :)


RE: ffmpeg script to convert files - movq - 15-03-2021

(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.)


RE: ffmpeg script to convert files - pfr - 15-03-2021

(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.


RE: ffmpeg script to convert files - ckester - 16-03-2021

(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!


RE: ffmpeg script to convert files - pfr - 16-03-2021

(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.