Skip to content

Instantly share code, notes, and snippets.

@lambdan
Created August 19, 2018 13:36
Show Gist options
  • Save lambdan/d8690adb959d8191eac190509eecabed to your computer and use it in GitHub Desktop.
Save lambdan/d8690adb959d8191eac190509eecabed to your computer and use it in GitHub Desktop.
extract snippets of video containing a certain phrase for instance
import os, sys, re, subprocess
tsfile = 'raws.txt'
# create the Timestamps (tsfile) using grep and Sublime Text or whatever you want
# it should contain the episode name/video file + the timestamp, for example:
#
# Seinfeld.S03E15.The.Suicide.720p.HULU.WEBRip.AAC2.0.H.264-NTb.srt-00:14:25,242 --> 00:14:27,775
# Seinfeld.S03E17.The.Boyfriend.720p.HULU.WEBRip.AAC2.0.H.264-NTb.srt-00:12:34,738 --> 00:12:36,639
# S03E18-00:12:34,768 --> 00:12:36,639
# ...
# one per line
# fiddle with the line.split()'s at line 28 and 31 if you use a different format
inputfolder = 'T:/TV Shows/Kitchen Nightmares (US)/' # where the original videos are
extension = '.mkv'
outputfolder = 'C:/Users/djs/Desktop/test5/' # where the extracted videos will be
def episode_from_filename(filename): # https://stackoverflow.com/a/9129611
episode = re.findall(r"(?:s|season)(\d{2})(?:e|x|episode|\n)(\d{2})", filename, re.I)
return "S" + episode[0][0] + "E" + episode[0][1]
with open(tsfile) as f:
lines = f.read().splitlines()
i = 0
for line in lines:
video = line.split(".srt-")[0]
text_episode = episode_from_filename(video)
timestamp = line.split(".srt-")[1].replace(",", ".")
start_time = timestamp.split(" --> ")[0]
stop_time = timestamp.split(" --> ")[1]
# find matching video
for root, dirs, files in os.walk(inputfolder):
for f in files:
if not f.endswith(extension):
continue
if episode_from_filename(f) == text_episode:
#print video
#print f
print dirs
inputvid = os.path.abspath(os.path.join(root, f))
outputvid = os.path.abspath(os.path.join(outputfolder, f + str(i) + '.mkv'))
subprocess.call(['ffmpeg', '-ss', start_time, '-to', stop_time, '-i', inputvid, '-c', 'copy', outputvid])
continue
i+=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment