Converting MKV to MP4 Using FFMPEG
ffmpeg -i "$f" -c:v copy -c:a aac -b:a 256k "${f%%mkv}mp4";&& rm "{$f}.mkv";
is a syntax error - you cannot have a &&
after a ;
. It should just be ffmpeg -i "$f" -c:v copy -c:a aac -b:a 256k "${f%%mkv}mp4" && rm "{$f}.mkv";
. Then, your $f
already ends in .mkv
, so the rm
command should be just rm "$f"
.
To recurse into subdirectories, use globstar
:
shopt -s globstar
for f in **/*.mkv; do ... done
All told:
#! /bin/bash
shopt -s globstar
for f in **/*.mkv
do
ffmpeg -i "$f" -c:v copy -c:a aac -b:a 256k "${f%%mkv}mp4" &&
rm "$f"
done
To run it on a schedule, see Running a script everyday using a cronjob or How do I properly install a systemd timer and service?.