:: krowemoh

Wednesday | 08 JAN 2025
Posts Links Other About Now

previous
next

Fixing Up Videos

2025-01-01

I love computers, opensource and nerds. Here is a project that I could probably wire up myself that would have taken me quite a bit of time but thanks to the community having already created each piece, all I had to do was chain them together.

There was a tv show that I wanted to watch that had been posted to youtube however it was in another language and so I need to get subtitles. Unfortunately no one has subbed it but there were auto-generated subtitles which are good enough.

I downloaded some episodes using yt-dlp and then found some subtitles on opensubtitles.org. However the subtitles were off by an appreciable amount and I didn't want to have to fudge each file myself.

Luckily there was a tool, ffsubsync that someone had made during a hackathon that solved my problem. It would analyze the video and sync the subtitle files to the video. It worked like magic.

The github page for ffsubsync goes into how it syncs the subtitles which is quite interesting and over my head but maybe someday I'll try to understand it. For now I can just blindly use it.

Once the episodes are downloaded and the subtitles are synced, I can then serve them out using Plex. Now I can watch this show as easily as anything on netflix.

Tools

The yt-dlp command looks like the following:

yt-dlp http://playlist.url.com

yt-dlp has quite a bit of options so take a look at the github page to see everything it can do.

The ffsubsync command looks like the following:

ffs file.mp4 -i unsyced.srt -o synced.srt

It is a python library so you will need to use pip to install ffsubsync and it relies to ffmpeg.

Copilot

I used chatgpt to help write a quick command that would let me quickly convert everything. This was interesting as I used copilot to do the initial script and then copied and pasted it into chatgpt and asked it to review it. This immediately proved my gut feeling right in that copilot had given me some bad code. It probably worked but I could tell something was off and that it had missed some ideas.

This is a bit disappointing as I only used to it generate the error checking and had expected it to be solid at that.

It was a very simple script that lets me enter just the episode name and then it would fill in the unsynced srt file and the new srt file. If everything went well, the script would also remove the unsynced file.

#!/usr/bin/bash

# Fix the subtitle file

filename=$1

if [ -z "$filename" ]; then 
    echo "Usage: fix-sub <filename>"
    echo "There should be a subtitle file with the same name as the video file ending with .unsync.srt"
    exit 1                                                                                                 
fi

if [ ! -f "$filename" ]; then
    echo "File not found: $filename"                                                                       
    exit 1
fi  

unsync="${filename%.*}.unsync.srt"
fixedFile="${filename%.*}.srt"

if [ ! -f "$unsync" ]; then
    echo "File not found: $unsync"                                                                       
    exit 1
fi

if [ -f "$fixedFile" ]; then
    echo "File already exists: $fixedFile"
    exit 1                                                                                                 
fi        

if ! command -v ffs &> /dev/null; then
    echo "Error: 'ffs' command not found."
    exit 1
fi

ffs "$filename" -i "$unsync" -o "$fixedFile"
if [ $? -ne 0 ]; then
    echo "Failed to fix the subtitle file"
    exit 1                                                                                                 
fi

rm "$unsync"