Skip to content

Instantly share code, notes, and snippets.

@Pullusb
Created July 18, 2016 20:42
Show Gist options
  • Save Pullusb/83b866ad335822bd122777f905f3f9eb to your computer and use it in GitHub Desktop.
Save Pullusb/83b866ad335822bd122777f905f3f9eb to your computer and use it in GitHub Desktop.
python script to batch harsub video using ffmpeg
import os, time
start_time = time.time()
#good tuning for compromise quality/weight
tuning = '-c:v libx264 -x264opts keyint=60 -pix_fmt yuv420p -preset medium -crf 19 -c:a copy -y'
ext = 'srt'
cwd = os.getcwd()
for i in os.listdir():
if i.startswith("sub_"):
for srt in os.listdir(i):
if srt.endswith(ext):
fullname = srt.split('.')[0] #sauce_EN
video = fullname.split('_')[0]#sauce
videoIn = video + '.mp4' #sauce.mp4
videoOut = fullname + ".mp4" #sauce_EN.mp4
fullpathVideo = os.path.join(os.path.abspath(i), videoIn)
# print (fullpathVideo)
fullpathSub = os.path.join(os.path.abspath(i), srt)
# print (fullpathSub)
fullpathOut = os.path.join(os.path.abspath(cwd), videoOut)
# print (fullpathOut)
os.chdir(os.path.abspath(i))
# print (os.path.abspath("."))
cmd = 'ffmpeg -i "{0}" -vf subtitles={1} {2} "{3}"'.format(fullpathVideo ,srt, tuning, fullpathOut)
print(cmd)
os.system(cmd)
os.chdir(os.path.abspath(cwd))
print ("elapsed time:", time.time() - start_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment