Last active
April 2, 2018 18:01
-
-
Save slabua/48c398660d66090797c9243862f7a437 to your computer and use it in GitHub Desktop.
Convert SubRip Text (SRT) files where multiple identical timestamps are used to show multiple-lined subtitle at the same time, to the standard SRT format with an unique timestamp for multiple-lined subtitles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # SubRip Text (SRT) Subtitle fixer script. | |
| # 2018 Salvatore La Bua slabua(at)gmail.com | |
| # | |
| # This script converts subtitle files where any multiple-lined subtitle to | |
| # appear on the screen at the same time is represented by the following | |
| # template: | |
| # | |
| # <line number 1> | |
| # <timestamp 1> | |
| # <text 1> | |
| # | |
| # <line number 2> | |
| # <timestamp 1> | |
| # <text 2> | |
| # | |
| # to the standard format as shown in the template below: | |
| # | |
| # <line number 1> | |
| # <timestamp 1> | |
| # <text 1> | |
| # <text 2> | |
| # | |
| # Usage: | |
| # ./subriptext_fixer.sh input_sub.srt output_sub.srt | |
| # | |
| input="$1" | |
| output="$2" | |
| echo "" > 1.txt | |
| echo "" > 2.txt | |
| totallines=$(($(wc -l < "$1") / 4)) | |
| for block in $(seq 1 $totallines) | |
| do | |
| echo $block | |
| item=1 | |
| ln=$(((((block - 1) * 4) + item) + 1)) | |
| sed -n "$ln"p $input >> 1.txt | |
| item=2 | |
| ln=$(((((block - 1) * 4) + item) + 1)) | |
| sed -n "$ln"p $input >> 2.txt | |
| done | |
| validline=0 | |
| for line in $(seq 1 $((totallines + 1 ))) | |
| do | |
| echo $line | |
| timestamp_new=$(sed -n "$((line))"p 1.txt) | |
| if [ $line -gt 1 ]; then | |
| timestamp_old=$(sed -n "$((line - 1))"p 1.txt) | |
| if [ "$timestamp_old" != "$timestamp_new" ]; then | |
| echo "" >> $output | |
| validline=$((validline + 1)) | |
| echo "$validline" >> $output | |
| echo "$timestamp_new" >> $output | |
| sed -n "$line"p 2.txt >> $output | |
| echo "$timestamp_new" | |
| else | |
| sed -n "$line"p 2.txt >> $output | |
| fi | |
| else | |
| echo "1" > $output | |
| echo "$timestamp_new" >> $output | |
| sed -n "$line"p 2.txt >> $output | |
| fi | |
| done | |
| sed -i s/\//g $output | |
| rm -f 1.txt 2.txt | |
| echo "Done." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment