Tech Giants' Competing Standards, Out-Of-Sync Subtitles And The FOSS Remedy

Tech Giants' Competing Standards, Out-Of-Sync Subtitles And The FOSS Remedy

Perhaps you know the situation: At one point in time you wanted to save precious storage space or you wanted to prevent the deterioration of physical media and you converted your DVD or VHS collection into files which you then stored on a hard drive or a cloud storage.

The other day, you wanted to watch one of your gems but, alas, the film is in a foreign language and when you stored it, you forgot to include the subtitles. "No problem", you think. Subtitles are easy to come by these days, e.g. from:

You download your subtitles, store them in the same directory as your film, sit down on your couch, open a bottle of wine and start to watch but, oh no, the timing seems wrong. "No problem", you think again: Most popular media players like VLC or Kodi have the option to set a time shift for the subtitle track. You fiddle a while, find the right offset, sit down again, but after a minute or two film and subtitles get out of sync again: one seems to be faster than the other!

Are The Subtitles Faulty?

Most probably not! You just have become a victim of a tech standards war from a hundred years ago: When electricity was the boom of the moment at the beginning of the XXth century and lightbulbs and electric machines started to pop up all over the place, the need for standardization arose. Unfortunately, companies didn't agree on a single one. So, in Europe, the German Allgemeine Electrizitäts Gesellschaft set the standard for AC to 220V at 50Hz, becoming the de facto global norm, whilst in the USA, Westinhouse opted for 110V at 60Hz, which can still be found in a handful of countries.

Meanwhile, the emerging film industry had set their standard to 24 photos per second. No problem at first, but when TV was invented, Germany opted for a 50 half-frames per second frequency, whilst the US, hardly surprising, went for 60 half-frames per second. At first, TV broadcating was a live event, only later, productions from the big screen were brought to the small one.

How To Take 24fps Cinema To 30/25fps TV?

The Americans decided to duplicate every fourth photo: 24/4 = 6, 6*5 = 30. The Germans, instead, decided to roll their projectors just 1/25 faster. Both systems worked but a 100 minutes film would be played on TVs following the German standard in 96 minutes. Pure TV productions also followed the standard. When you want to convert a NTSC (TV standard in the US and some other countries) production into PAL (TV standard elsewhere), you take out the duplicate images and increase the playback speed, besides other things.

Now, What Does This Means For My Subtitles?

If your video comes from a PAL source but your subtitles were time coded from a NTSC source, your subtitles will be 4% slower than the video. In contrast, if your video comes from a NTSC source but your subtitles were time coded from a PAL source, they will be displayed 4% faster than the film.

What Is The Solution?

Adjusting Video Speed

One solution could be to recode your video material with the fabulous package

If you wanted to, say, slow down your video by 4%, you could do the following:

ffmpeg -i my_video.mp4 -filter:v "setpts=1.04*PTS" my_slower_video.mp4

However, this will take quite some time and it will probably not improve the video’s quality.

Adjusting Subtitle Speed

The subtitle files `*.srt` are basically text files containing timestamps. These can easily be adjusted without any implications for the video's quality.

You can use the following script to do that:

#!/usr/bin/perl -w

# © by Mina, 2020
# Free software under the GNU public licence 3.0 
# https://www.gnu.org/licenses/gpl-3.0.en.html

use feature 'say';
@ARGV or die "No parameters given! " .
  "Usage: subspeed.pl filename [+|-] > outfile.srt\n";
my ($filename, $speed) = @ARGV;
die "no such file: $filename\n" unless -e $filename;
die "no valid speed parameter given! Usage: subspeed.pl filename [+|-] > ".
  "outfile.srt\n" unless $speed =~ /\+|-/; 
my $speedFunc = $speed =~ /\+/ ? sub { 
  return 25/24 * $_[0] 
} : sub { 
  return 24/25 * $_[0] 
};

open(my $fh, '<', $filename);
while(<$fh>) {
    unless(m/^\s*(\d+:\d\d:\d\d,\d+)\s+--\>\s+(\d+:\d\d:\d\d,\d+)\s*$/) {
        print;
        next
    }
    say join(' --> ',
      ( map { ms2ts($_) } ( map { 
         &$speedFunc($_) } ( map { 
           ts2ms($_) } ($1, $2) ) ) ) );
}
close($fh);

sub ts2ms { 
    my $ts = shift;
    $ts =~ s/^\s+//;
    $ts =~ s/\s+$//;
    my ($h, $m, $s) = split(':',$ts);
    my ($sec, $ms) = split(',',$s);
    $ms += 1000 * ( $sec + 60 * ( $m + 60 * $h));    
    return $ms
}

sub ms2ts {
    my $t = shift;
    $t = int($t);
    my $ms = $t % 1000;
    $t = int($t/1000);
    my $s = $t % 60;
    $t = int($t/60);
    my $m = $t % 60;
    my $h = int($t/60);
    $ms = '0' x ( 3 - length($ms) ) . $ms;
    s/^(\d)$/0$1/ foreach $h, $m, $s;
    return "$h:$m:$s,$ms"
}

Just copy it into the editor of your choice, save it as subspeed.pl, deploy it on your system via
sudo cp subspeed.pl /usr/local/bin and make it executable via
sudo chmod a+x /usr/local/bin/subspeed.pl.

Now, if you want to speed up your subtitles, you just type:

subspeed.pl my\ favourite\ movie.srt - > my\ favourite\ movie.faster.srt

or, if you want to slow them down:

subspeed.pl my\ favourite\ movie.srt + > my\ favourite\ movie.slower.srt

- stands for decrease timestamps, + for increase timestamps.

EDIT: I had to break some lines, i.e. introduce some newlines which might make the code less readable because the lines were too long to be displayed correctly.

4 Likes

Great explanation & solution. Had this issue so many times, but wasn’t aware of the standardization bullshit in the movie industry, especially since I thought that PAL & NTSC are only related to old videos, not new ones.

Will try out the solution, next time I will experience this trouble, again.

2 Likes

Actually, this thing I described happened, when we were watching an episode of “The Sopranos” (to my shame, not from a DVD but from the internet - fortunately this is legal in Switzerland) the other day. The subtitles lagged behind the video, about two seconds per minute.

With the method above ("-" parameter), I managed to fix them, and they matched perfectly then.

Let me know whether it works for you or whether you encounter problems.

1 Like

Or install Handbrake and re-encode film with added Subtitles. That’s what I do and Handbrake gets it right first time. Yes waiting for film to encode can be time consuming, but with Handbrake you have the choice of choosing what quality you want and what format. ISO, MP4 etc

This would mean for me that I’d have to waste a lot of space, because I’d need to have a video without and with hard-coded subtitles. I don’t like subtitles because I always read them, even when I don’t need them and they distract me from the medium. I only use subtitles if I’m not watching alone and it is absolutely necessary for the other people to have subtitles. Therefore, hard-coded subtitles are no option for people like me.

Of course: There’s always more than one way to skin a cat.

However: My main goal was to tell a (hopefully) interesting story about how commercial interests in setting proprietary standards at the beginning of the last century still impact us in the digital age. The recipe at the end was merely meant as the cream on top.

Still, I tend to agree with @Akito: Hard coded subtitles have several disadvantages:

  1. They are ugly: When I watch, say, a period drama, I don’t want them to cover the beautiful costumes. At least, I want to have the option to turn them off when not needed.

  2. You have to be very careful with the sync. Perhaps, your subtitles are for another cut than the actual film. With separate subtitles, you can just adjust the offset in the middle off the film. In order to make sure, they fit, you have to check at least the beginning and the end of the film before actually watching it.

  3. Recoding a film is a time-consuming matter: Even on my powerful desktop computer, re-rendering a 100 minutes film (in HQ) takes at least 15 minutes of real time. Just adjusting the subtitles with the script above takes only the fraction of a second.

  4. I would always try to avoid altering the video source unless absolutely necessary: It means decompressing, dropping or adding frames and compressing again. All this comes with a loss in quality.

That said: Handbrake is indeed a wonderful GUI for ffmpeg, but many operations are actually done easier and quicker on the command line.

That is another big point!

I think everyone here knows the good old times when videos online were super low quality. However, as I have too often experienced, this was most of the time due to the fact that doing stuff on the computer was new to a lot of users, so they used some generic video editing software, which compressed this video. Then someone else editted it again and it was further compressed. So the quality of the original video might’ve been not so bad, but if you constantly transcode media files, there is often the possibility, that something gets lowered in quality, even if it is only the audio. Now, even if you specifically check if the quality stays the same, some software is so complicated that you might not catch a setting that actually lowers the quality of the video.

That said, all this applies to people who very rarely edit video files, like for example, myself.

P.S.:
A fun fact regarding this topic is also, that some time ago on YouTube (perhaps they improved it now) you had to upload Full HD or better quality videos, where only the music was important, because if you lowered the video quality, like setting the resolution too low, then YouTube was compressing the audio automatically, to a value fitting the resolution you chose. So even if you did not need any video displayed, you still had a quality loss when uploading low resolution videos of music.

1 Like

Yes a bit like the Human Genome, keep replicating it, as we have seen with AD-HD (Attention Deficit Hyper-Activity Disorder.) in children, that’s becoming more and more common, which to my mind is the human genome wearing out. Never knew about it in my day when I was at school.

Yes, but it heavily depends on many aspects. Back then people were more tempted to just call those hyper active kids “clowns” or whatever and that was it. They did not think that it is a serious disease or anything of that sort. Similar things happened to other diseases.

Another example is that people often say, that cancer is a thing of our time and never happened before. This is wrong, because people just tend to get much older than they did 100 or 200 years before. So they did not even reach an age where cancer is barely common. Our bodies are just not made to last that long. Our biology expects us to live 25-35 years. Anything beyond that is a gamble, from a biological and evolutionary perspective.

3 Likes

It hardly seems believable to me that four billion years of evolution should suddenly be ruined by a couple of generations with modern medicine.

Many of the issues, you might see today, are just a matter of better diagnostics. Having suffered from ADD my whole life but only being diagnosed at beyond more than 40 years of age, I can only say: If I had got the treatment today’s children have at an earlier stage of my life, I wouldn’t have needed to go through many avoidable hardships.

It’s not the worst thing there is, but AD(H)D is no fun at all.

1 Like

Blimey I’m so sorry, I knew my analogy would get me in trouble someday :weary: Do you take anything for it or just get on with it?? I work with vulnerable adults and have worked with children in the past with ADHD, so have seen up close and personal what it’s like. I must admit that even though I am a carer, care about everything in life, there were days when I could see myself pulling my hair out and over the years have lost a lot of hair. Nothing to do with the head board on my bed after all, as everyone I know thought. :smiley: Diagnosing must be the hardest thing ever? As there are key tests to be done, with key answers. Everyone is different though. Diagnosing Dementia to which type is blooming near impossible and ADD and ADHD must be hard to distinguish between the two?

I do wish everyone on this Forum and through out the Internet a hopefully happier new year.

1 Like

I agree, AD(H)D is extremely hard to diagnose, especially only tendencies. If you have trouble concentrating/focusing on some mindful tasks, often people tend to think that these people are just stupid or dumb. However, sometimes it’s just the case that they have a slight ADD-like tendency, which makes it hard for them to focus on a single subject for too long, by e.g. getting distracted all the time.

1 Like

I get distracted all the time and end up going back to what I was previously doing, though takes a while to remember what it was? Sometimes having to really think back to what it was I was doing in the first place, usually a distraction can be as simple as a color that catches the person’s eye, a piece of writing anything that reminds that person about something else, leading to distraction. Distraction is used a lot in the care field too, when dealing with Dementia, to distract the person from harming themselves or others. Sorry this has gone off topic in hand, but ain’t life amazing to think that from subtitles to ADD topics can flow from one subject to the next just like that, or that, or even that. :smiley:

3 Likes

I totally agree: That’s the miracle of human communication!

Not wanting to dive further into the subject, I can only say that proper medication really is a quantum leap in quality of life for affected persons.

2 Likes

Recently, I wanted to change the quality of a video file, so I used Handbrake to re-encode it. It took almost FOREVER. This single ~5GB file easily took an hour (or two?) to convert to a lower quality. This is an insanely huge amount of time, considering I’ve got hundreds of videos with at least such a size.
Therefore, Handbrake is definitely not a valid alternative in the case specified in this thread.

P.S.:
I have a pretty fast computer. Hardware is not an issue in this re-encoding example.

P.P.S.:
I’ve re-encoded the same video into a higher quality. This time I watched the clock. It took about 55 minutes. Still way too much time.

You can lower the quality slightly to speed things up a bit. I love Handbrake as it does encrypted DVDs too.

I already had the quality lowered below average. Going even lower wouldn’t be acceptable.

The program seems very nice and I think I’ll use it when I need it. Still, I wouldn’t use it for hardcoding subtitles into videos for hours and hours on.