1

I have a Windows batch script that I want to run on Linux, but I'm having trouble converting it to a shell script

I wonder if anyone can help.

:start
for /r %%F in (*.mkv) do (
C:\Python27\Scripts\ffmpeg\bin\ffmpeg.exe  -y -i "%%F" -c:v libx264 -preset ultrafast -minrate 4.5M -maxrate 4.5M -bufsize 9M -c:a ac3 "%%~dpnF.mp4"
if not errorlevel 1 if exist "%%~dpnF.mp4" del /q "%%F"
)
TIMEOUT /T 60
goto start

I've been trying to convert it to a shell script, but I'm having trouble. I'm not great at this.

for f in {*.mkv,*/*.mkv,*/*/*.mkv,*/*/*/*.mkv}; do 
ffmpeg -i "$f" -c:v libx264 -preset ultrafast -minrate 4.5M -maxrate 4.5M -bufsize 9M -c:a ac3 "${f%mkv}mp4";
rm "$f";

I'm not sure how to loop it, so its constantly checking.

1
  • The "Linux script" term is incorrect. It's "shell script". Please add the name of the shell that you're using and edit the tittle.
    – Biswapriyo
    Commented Feb 9, 2020 at 17:11

1 Answer 1

0

More like (assuming bash):

shopt -s globstar
for f in **/*.mkv
do 
    ffmpeg -i "$f" -c:v libx264 -preset ultrafast -minrate 4.5M -maxrate 4.5M -bufsize 9M -c:a ac3 "${f%mkv}mp4";
    [[ $? -eq 0 ]] && rm "$f";
done

In practice:

  • You were missing a done statement indicating the loop
  • Your {*.mkv,*/*.mkv,*/*/*.mkv,*/*/*/*.mkv} pattern while not incorrect will make ffmpeg issue an error message for each pattern that doesn't match. globstar together with ** makes a single pattern work across directory levels.
  • I added a test at the end to erase the .mkv only if there was no error when transcoding to mp4.
2
  • Thanks ill give this a try and get back to you thank you, how would go about running this constantly so it constantly checking for mkv's from the top level directory Commented Feb 9, 2020 at 16:57
  • im getting this error in when running it [matroska,webm @ 0x43468c0] Format matroska,webm detected only with low score of 1, misdetection possible! Truncating packet of size 1336371 to 152 [matroska,webm @ 0x43468c0] EBML header parsing failed ffmpeg/ffmpeg/tests/ref/lavf-fate/av1.mkv: Invalid data found when processing in put Commented Feb 9, 2020 at 17:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .