Skip to content

Instantly share code, notes, and snippets.

@mluis7
Created April 12, 2024 00:11
Show Gist options
  • Save mluis7/e19571f2c7284e60d8701c1c7f5c9615 to your computer and use it in GitHub Desktop.
Save mluis7/e19571f2c7284e60d8701c1c7f5c9615 to your computer and use it in GitHub Desktop.
Apply and synchronize 2 srt subtitle files to a single video
#!/bin/bash
readme=$( cat <<README
Problem:
How to apply and synchronize 2 srt subtitle files made for CD release to a single video ripped from DVD/BluRay.
No fps conversion so subtitles and video must have same fps (23.976, 25, whatever)
1 ~2h video ripped from DVD or BlueRay
2 subtitles files for CD box release of same movie.
# Start of sub1
1
00:02:03,506 --> 00:02:08,500
LLUEVE Y BRILLA EL SOL
# End of sub1
...
228
01:09:46,430 --> 01:09:49,831
Me obliga a pintar. No puedo perder
el tiempo hablando contigo.
# start of sub2
00:01:48,116 --> 00:01:52,314
EL FUJIYAMA EN ROJO
Manual steps:
Step 1) Use sub1 (CD1) with the movie to find 'final_delay' to apply.
Subtitle might appear before or after the dialog. That's the "delay" to apply, positive or negative.
Step 2) Find sub2 start time and set 'sub2_start' variable with it. (optional)
Step 3) Find end of sub1. sub2 should be applied around past this time. e.g.:
01:09:49
Fast forward the movie to that time to find the sub2 starting time.
Let's say next dialog or screen title happens at:
01:13:20
set 'sub2_start_vid' variable with it.
Step 4) Optional: if files are already utf-8 encoded then remove '-sub_charenc iso-8859-15' from commands below
See Bash code below for each step.
Step 1) Convert sub1 to UTF-8 (optional).
Step 2) Convert start time of sub2 to seconds
Step 3) Shift sub2 to start at 0 using seconds from step 2
Step 4) Appy offset from Manual Step 3 to sub2
Step 5) Concatenate subtitles into a single subtitle file
Step 6) Apply final offset to file from Step 6
Set 'sub2_start', 'sub2_start_vid', 'final_delay' variables to values found above and run the script
passing path to both files and extension if it's not srt.
./merge-srt-cd-subtitles.sh [sub_file1.srt] [sub_file2.srt] [.srt]
That's it. The final subtitles file can be used on the single video.
:-)
README
)
if [ "$1" = "-h" ]; then
echo -e "$readme\n"
exit
fi
sub1='cd1.srt'
sub2='cd2.srt'
ext='.srt'
if [ -f "$1" ]; then
sub1="$1"
fi
if [ -f "$2" ]; then
sub2="$2"
fi
if [ -n "$3" ]; then
ext="$3"
fi
sub1_base=$(basename "$sub1" "$ext")
sub2_base=$(basename "$sub2" "$ext")
# Manual Step 1
final_delay='11.1'
# Manual Step 2 (optional)
sub2_start="$(sed -nre '2 { s/(00:[^,]+),.*/\1/p }' $sub2)" #00:01:48
# Manual Step 3
sub2_start_vid='01:13:20'
# Step 1
ffmpeg -hide_banner -y -sub_charenc iso-8859-15 -i "$sub1" "$sub1_base"-utf-8.srt
# Step 2 - seconds to advance CD2 subtitles to start at 0
sub2_start_secs=$(date -d "1970-01-01 $sub2_start -00" '+%s')
# Step 3 - shift CD2 subtitles to start at 0 time
ffmpeg -hide_banner -y -sub_charenc iso-8859-15 -itsoffset -"$sub2_start_secs" -i "$sub2" "$sub2_base"-utf-8-shift-back.srt
# Step 4 - shift CD2 subtitles to start at video time
ffmpeg -hide_banner -y -itsoffset "$sub2_start_vid" -i "$sub2_base"-utf-8-shift-back.srt "$sub2_base"-utf-8-shift-final.srt
# Step 5 - concatenate CD1, CD2 subtitles
cat "$sub1_base"-utf-8.srt "$sub2_base"-utf-8-shift-final.srt > "$sub1_base"-"$sub2_base"-final.srt
# Step 6) Finally, Convert to ssa and shift to sinchronize with video
ffmpeg -hide_banner -y -itsoffset "$final_delay" -i "$sub1_base"-"$sub2_base"-final.srt movie-subs-utf-8-shift.ass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment