Skip to content

Instantly share code, notes, and snippets.

@nadhirxz
Last active January 8, 2024 23:34
Show Gist options
  • Save nadhirxz/ceae6431a1176aa7280d65b78889d7cb to your computer and use it in GitHub Desktop.
Save nadhirxz/ceae6431a1176aa7280d65b78889d7cb to your computer and use it in GitHub Desktop.
Scripts to convert and format subtitles using SubtitleEdit on windows
  1. Install SubtitleEdit.
  2. Install Python 3.
  3. Set subtitleedit variable in both sub_format.bat and substationalpha2srt.bat to the path of the executable of SubtitleEdit.
import sys, os, re
if (len(sys.argv) > 1 and sys.argv[1] == '-en'): LANGUAGE = 'en'
else: LANGUAGE = 'ar'
STYLE = {'ar': 'Adobe Arabic,28', 'en': 'Open Sans Semibold,24'}
SUBS_FOLDER='subs'
for file_name in os.listdir(SUBS_FOLDER):
if (file_name.endswith('.ass')):
file_name = f'{SUBS_FOLDER}/{file_name}'
lines = open(file_name, 'r', encoding='utf-8').read()
lines = re.sub(r'Style:[^\n]+', f'Style: Default,{STYLE[LANGUAGE]},&H00FFFFFF,&H0300FFFF,&H00000000,&H02000000,-1,0,0,0,100,100,0,0,1,1.8,0,2,10,10,15,1', lines)
open(file_name, 'w', encoding='utf-8').write(lines)
@echo off
set subtitleedit="C:\Program Files\Subtitle Edit\SubtitleEdit.exe"
if not exist srt call substationalpha2srt
set output=subs
if not exist %output% mkdir %output%
%subtitleedit% /convert srt/*.srt AdvancedSubStationAlpha /outputfolder:"%output%" /overwrite %*
style_subs.py %*
@echo off
set subtitleedit="C:\Program Files\Subtitle Edit\SubtitleEdit.exe"
set output=srt
if not exist %output% mkdir %output%
%subtitleedit% /convert *.srt subrip /RemoveFormatting /MergeSameTexts /outputfolder:%output% /overwrite
%subtitleedit% /convert *.ass subrip /RemoveFormatting /MergeSameTexts /outputfolder:%output% /overwrite
if exist *.srt (
xcopy *.srt original-subs\
del *.srt
)
if exist *.ass (
xcopy *.ass original-subs\
del *.ass
)
@AdventurerRussia
Copy link

is it possible for a script that shifts subtitles 1 second backwards or forwards?

@nadhirxz
Copy link
Author

nadhirxz commented Jan 8, 2024

is it possible for a script that shifts subtitles 1 second backwards or forwards?

You can do that by using /offset:hh:mm:ss:ms, so if I want to shift subs forward by let's say 1 minute and 3 seconds, I'd do it like this:

subtitleedit /offset:00:01:03:00

To shift backward you just add a - sign to the offset

subtitleedit /offset:-00:00:05:00

It's also supported by the script:

sub_format /offset:00:00:01:00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment