0

I have a basic command (ffmpeg -i input_file out.srt) to turn .mkv video files to .srt subtitle files. The problem is that I have to manually run the command for every .mkv file. So I tried to implement a for loop in Bash, but I keep getting errors.

 #!/bin/bash
 # ffmpeg -i input_file out.srt


 for i in *.mkv ; do
      ffmpeg -i "$i" "$(basename "${i/.mkv)")".str
      sleep 30
 done

The two errors I get are:

./subcon.sh: line 6: unexpected EOF while looking for matching `}'
./subcon.sh: line 9: syntax error: unexpected end of file

I am not familiar with Bash to understand whats going on. Does anyone know where I can look stuff up or how to solve this particular problem?

llogan
  • 121,796
  • 28
  • 232
  • 243
VickTree
  • 889
  • 11
  • 26
  • For the basename parts, see https://stackoverflow.com/questions/12152626/how-can-i-remove-the-extension-of-a-filename-in-a-shell-script/12152997 – Charles Duffy Aug 26 '19 at 17:47

1 Answers1

1

Problems:

  • Mismatched brackets. Change { to (.
  • Incorrect subtitle extension. Change str to srt.
  • basename syntax is incorrect. Change "$(basename "${i/.mkv)")".str to "$(basename "$i" .mkv)".srt. Or use Bash parameter expansion instead of basename.

New script:

#!/bin/bash
# ffmpeg -i input_file out.srt

for i in *.mkv ; do
     ffmpeg -i "$i" "$(basename "$i" .mkv)".srt
     sleep 30
done

You can eliminate basename:

#!/bin/bash
# ffmpeg -i input_file out.srt

for i in *.mkv ; do
     ffmpeg -i "$i" "${i%.*}.srt"
     sleep 30
done

I recommend shellcheck.net to check your Bash scripts.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • Are these issues not all under the typo category, as discussed in #2 in the "some questions are still off-topic" list in https://stackoverflow.com/help/on-topic? (Whereas the "where I can look stuff up" aspect of the request is #4 in the same list). – Charles Duffy Aug 26 '19 at 17:38
  • @CharlesDuffy #2 says "a" typo, and there were two, so therefore it is exempt. Jokes aside, I think it was more of a `basename` syntax/misusage issue with two additional typos two deal with. There were enough issues here that simply closing as a typo would leave the user stranded. – llogan Aug 26 '19 at 17:45
  • That part can be handled with a close-as-duplicate naming an existing question about how to remove file extensions, with answers both showing correct basename use and the given parameter expansion. (That said, I already gave a VTC with the typo-based reason, so can no longer use my dupehammer here). – Charles Duffy Aug 26 '19 at 17:47