How to convert multiple subtitle files

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Pippin
Level 4
Level 4
Posts: 441
Joined: Wed Dec 13, 2017 11:14 am
Location: The Shire

How to convert multiple subtitle files

Post by Pippin »

Hi,

Trying to convert multiple *.vtt to *.srt subtitle files using ffmpeg.
A single file works fine:

Code: Select all

/usr/bin/ffmpeg -i vttfile.vtt srtfile.srt
For multiple files i have the following:

Code: Select all

#!/bin/sh

LOC="$HOME/Videos/"

for x in "$LOC/*.vtt"
    do /usr/bin/ffmpeg -i "$LOC/$x" "$LOC/$x.srt"
done
Execution:

Code: Select all

sh -v subconv.sh
Results in:

Code: Select all

*.vtt: No such file or directory
Executing as user or root makes no difference.
Example of subtitle file:
Name. Serie. Episode. English.en.vtt
Wanted result:
Name. Serie. Episode. English.en.srt
Thanks.

Edit: Typo
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
I gloomily came to the ironic conclusion that if you take a highly intelligent person and give them the best possible, elite education, then you will most likely wind up with an academic who is completely impervious to reality.
Halton Arp
donalduck
Level 4
Level 4
Posts: 236
Joined: Mon Oct 07, 2013 1:43 pm
Location: there

Re: How to convert multiple subtitle files

Post by donalduck »

Hello,
several mistakes in your script I think :)

try this

Code: Select all

#!/bin/sh
LOC="$HOME/Videos"
for x in $LOC/*.vtt
  do /usr/bin/ffmpeg -i "$x" "${x%vtt}srt"
done
also you can use -x option to debug a shell script.

more explanation: man sh
User avatar
root
Level 3
Level 3
Posts: 133
Joined: Wed Mar 13, 2019 11:57 am

Re: How to convert multiple subtitle files

Post by root »

123.png
do not use quotes
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How to convert multiple subtitle files

Post by rene »

Very slight adjustment to above answers; don't use quotes around wildcards. That's to say, if indeed you want to guard against $LOC containing whitespace you'd write it as e.g. for x in "$LOC"/*.vtt. In this case it's clearly not important, $LOC being defined without whitespace just above, but this additional reply so as to say that quotes by design "take away special meaning" in shell, and specifically, take away the special meaning of *.
User avatar
root
Level 3
Level 3
Posts: 133
Joined: Wed Mar 13, 2019 11:57 am

Re: How to convert multiple subtitle files

Post by root »

rene wrote: Wed May 15, 2019 8:33 pm Very slight adjustment to above answers; don't use quotes around wildcards. That's to say, if indeed you want to guard against $LOC containing whitespace you'd write it as e.g. for x in "$LOC"/*.vtt.
Adjustment? Can the home folder contain spaces in the name?! No! You say rational things, but the word "adjustment" is inappropriate here. It is rather an "addition".
gm10

Re: How to convert multiple subtitle files

Post by gm10 »

root wrote: Thu May 16, 2019 4:49 am Can the home folder contain spaces in the name?! No!
Why would you think that? Of course it can. Try it:

Code: Select all

sudo adduser spaces --home="/home/Home Directory With Spaces/"
User avatar
Pippin
Level 4
Level 4
Posts: 441
Joined: Wed Dec 13, 2017 11:14 am
Location: The Shire

Re: How to convert multiple subtitle files

Post by Pippin »

Hi,

Thanks all, little late reaction from me but donalduck's way works here.
In the meantime I found VLC will handle vtt files so problem solved.

Thanks.
I gloomily came to the ironic conclusion that if you take a highly intelligent person and give them the best possible, elite education, then you will most likely wind up with an academic who is completely impervious to reality.
Halton Arp
gm10

Re: How to convert multiple subtitle files

Post by gm10 »

Pippin wrote: Thu May 16, 2019 10:19 am In the meantime I found VLC will handle vtt files so problem solved.
Don't all media players? At least mpv does as well.
User avatar
Pippin
Level 4
Level 4
Posts: 441
Joined: Wed Dec 13, 2017 11:14 am
Location: The Shire

Re: How to convert multiple subtitle files

Post by Pippin »

Hi,

VLC finds the subs automatically if the file name is the same.

Both Media Player and mpv Media Player not working.
Cannot find any menu in mpv.

Thanks.
I gloomily came to the ironic conclusion that if you take a highly intelligent person and give them the best possible, elite education, then you will most likely wind up with an academic who is completely impervious to reality.
Halton Arp
gm10

Re: How to convert multiple subtitle files

Post by gm10 »

Pippin wrote: Thu May 16, 2019 1:36 pm VLC finds the subs automatically if the file name is the same.

Both Media Player and mpv Media Player not working.
Cannot find any menu in mpv.
Weird. I don't have Media Player but mpv behaves just like the VLC functionality you describe (actually it also picks them up as filename.languagecode, that's the preferred naming because then it can display the subtitle language in the UI).

mpv has a minimalist interface so there is no menu, the subtitle selection is at the bottom of the OSD, to the left of the volume control:
mpv-subtitles.png
mpv-subtitles.png (6.77 KiB) Viewed 2762 times
And further to the left is the audio track selection.

But there's nothing wrong with using VLC, not trying to talk you out of it.
User avatar
Pippin
Level 4
Level 4
Posts: 441
Joined: Wed Dec 13, 2017 11:14 am
Location: The Shire

Re: How to convert multiple subtitle files

Post by Pippin »

Hi,

Your example showing 1/2 is showing -/0 for me.
mpvsubs.png
mpvsubs.png (17.97 KiB) Viewed 2754 times
I gloomily came to the ironic conclusion that if you take a highly intelligent person and give them the best possible, elite education, then you will most likely wind up with an academic who is completely impervious to reality.
Halton Arp
gm10

Re: How to convert multiple subtitle files

Post by gm10 »

Yep, if the filename of the subs is the same but it doesn't show I have no answer for you. You could force it to load via the command line option I suppose, but that's inconvenient.

Note that I'm running the latest release version (0.29.1) rather than the older 0.27.2 in the LM 19 repos, so maybe that's the reason, although I saw nothing in the changelog about subtitles.
Locked

Return to “Scripts & Bash”