Categories
Linux Useful

Convert Subtitles SRT from ISO-8859-16 to UTF-8 – iconv – Linux

On the current personal NAS server, which is more like a Media Server for a Home Theater, I installed Jellyfin instead of more “popular” but not one of the most suitable for Home Media Center, such as Open Media Vault, or Amahi.

The current configuration replaced an older server for which I used OMV (Open Media Vault) for two years, but which in the latest variants came more “empty” than usual, fewer plugins and other options that practically did not have the purpose of a Media Center.

On the web interface at Jellyfin I encountered a small problem with subtitles in the my native language, the player has no option to choose the necessary character set, which is hard to believe, any decent media player has these options, but the programmers who develop Jellyfin they thought it was not a necessary option: WRONG!

One of the developers suggested on a forum that users should convert subtitles to UTF8 format from ISO-8859- 16 which is unbelievable! How can you ask users to do such a thing when most of them do not know how to do such a thing and it is difficult to do that when you have many hard drives and maybe hundreds of subtitles!

But in the end, their motivation doesn’t matter anymore, I solved it with the conversion, but this only temporarily, because every subtitle that comes with a movie or I find it online is not UTF8, none … so I’ll have to do this indefinitely, uffff … it’s hard to believe that there are programmers who don’t give a damn about the users, the true beneficiaries of their work!

The conversion is relatively simple, I used a single command line that is used in the basic folder of media files, its effect being recursive:

@ find . -type f -iname "*.srt" -and -not -iname "*.ro.srt*" -exec sh -c 'iconv -f ISO-8859-16 -t UTF-8 "{}" > "{}.ro.srt"' \;

As you can see, the destination files must be renamed with a 2-letter ISO country code extension, .ro.srt, therefore the command line must be modified correctly, so that at the next use of the command it does not convert the already converted files. I didn’t delete the original files because you don’t know when I need them anymore!

Here is an example for English 2-letter ISO country code:

@ find . -type f -iname "*.srt" -and -not -iname "*.en.srt*" -exec sh -c 'iconv -f ISO-8859-16 -t UTF-8 "{}" > "{}.en.srt"' \;

The two-letter ISO country extension is required for the Jellyfin player to find the correct subtitle in the configured language, otherwise it is “undefined” or “off”!

If you want, you can also make a batch file (shell script), for example iso2utf.sh, which you can execute in the same way, in the base folder:

#!/bin/bash\
find . -type f -iname "*.srt" -and -not -iname "*.ro.srt*" -exec sh -c 'iconv -f ISO-8859-16 -t UTF-8 "{}" > "{}.ro.srt"' \;
exit 0

And after saving you must make it executable:

@ sudo chmod +x iso2utf.sh
@ sudo chmod 755 iso2utf.sh

and can be executed as follows:

@ ./path/to/your/script/iso2utf.sh

or add a symbolic link to Local binaries:

@ ln -s /path/to/your/script/iso2utf.sh /usr/local/bin

And it can be easily executed:

@ .iso2utf.sh

After & Before

ISO-to-UF8-Subtitle-Conversion

Update:

Recently I found another solution for shell script, which is really longer than the previous version, not just a single line, but eliminates a doubling of the initial file extension!

#!/bin/bash
find ${PWD} -type f | \
 (while read file ; do
      if [[ "$file" == *.srt* ]]; then
          if [[ "$file" != *.ro.srt* ]]; then
              /usr/bin/iconv -f ISO-8859-16 -t UTF-8 "$file" > "`dirname "$file"`/`basename "$file" .srt`.ro.srt"
          fi
      fi
done);

And to get rid of the previously created files, I used the solution presented here: How to Remove Files Recursively using Wildcard?

P.S. However, if the player has several subtitles in the same folder, doesn’t find the correct one, either it looks OFF or Undefined!

Leave a Reply

Your email address will not be published. Required fields are marked *