1

I'm using the following line to convert

for f in *.mkv; do ffmpeg -i "$f" -c:v copy -c:a aac -b:a 256k "${f%%mkv}mp4";&& rm "{$f}.mkv"; done

i need this to also check sub directories but i keep getting token error or bash error

also it doesn't delete the mkv files when finished converting

once i have the line working correctly how would i then go one step further and make it auto run say once a day to convert any newly added mkv files.

muru
  • 207,228
playl01
  • 29

1 Answers1

7

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

muru
  • 207,228