Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- ###############################################################################
- # process-tv.py Wes Brown, (c) 2009
- # Takes for input a directory containing DVD rips. Rips are assumed to be in
- # the format of SeriesName-S1-D1. Cannot have space in rip names.
- #
- # Freely distributable as long as there is attribution.
- ###############################################################################
- import re
- import sys
- import os
- import subprocess
- # Our directory to scan for DVD rips.
- path = sys.argv[1]
- # This routine scans the titles available on the DVD rip. It looks for
- # available English subtitles, and finds the length of each DVD title.
- # It will only return titles that have subtitles or captions.
- def AutodetectTracksSubtitles( vobFile ):
- queryResult = subprocess.Popen(['HandbrakeCLI', '-t', '0', '-i',
- vobFile], stdout=subprocess.PIPE,
- stderr=subprocess.PIPE ).communicate()
- resultLines = queryResult[1].split("\n")
- returnArray = {}
- titleNo = []
- captionTrack = []
- subtitleTrack = []
- seconds = []
- for line in resultLines:
- titleQuery = re.findall("\+ title (\d+)", line)
- if titleQuery:
- # Flush out our current status.
- if titleNo:
- # Our preferred default is closed captions.
- if captionTrack != []:
- returnArray[ titleNo ] = \
- [ captionTrack, seconds ]
- # If there are no closed captions, fall back to
- # subtitles.
- elif subtitleTrack != []:
- returnArray[ titleNo ] = \
- [ subtitleTrack, seconds ]
- # If there are no subtitles or closed caption,
- # it is probably a junk track. Do not return.
- else:
- returnArray[ titleNo ] = None
- # Flush for the next title.
- captionTrack = []
- subtitleTrack = []
- seconds = []
- titleNo = titleQuery[0]
- # Find out which subtitle tracc has closed captions.
- captionTrack = re.findall("(\d+), Closed Captions \(iso639-2: eng\) \(Text\)", line)
- if captionTrack:
- captionTrack = captionTrack[0]
- # Find out which subtitle track has English subtitles.
- englishSubtitleLine = re.findall(
- "\(iso639-2: eng\)", line )
- if englishSubtitleLine:
- subtitleTrack = re.findall("\+ (\d+),", line)[0]
- # Let's figure out how long our title is.
- durationLine = re.findall("\+ duration: (\d+):(\d+):(\d+)", line)
- if durationLine:
- hours, minutes, seconds = durationLine[0]
- seconds = int(hours) * 60 * 60 + int(minutes) * 60 + \
- int(seconds)
- # Do a final flush before returning the array.
- if titleNo:
- if captionTrack != []:
- returnArray[ titleNo ] = [ captionTrack, seconds ]
- elif subtitleTrack != []:
- returnArray[ titleNo ] = [ subtitleTrack, seconds ]
- else:
- returnArray[ titleNo ] = None
- return returnArray
- for filename in os.listdir( path ):
- # The assumption is that DVD rips are in directories in the format of:
- # SeriesName-S1-D1 - name, season, and disk.
- fileInfo = filename.split('-')
- # We don't want to try to transcode using already transcoded files.
- if len(fileInfo) == 3 and not( ".m4v" in filename ):
- vobFile = path + '/' + filename
- show, season, disk = fileInfo
- # Get our subtitles and valid titles.
- subtitleTracks = AutodetectTracksSubtitles( vobFile )
- discTitles = []
- for track in subtitleTracks:
- if subtitleTracks[track] != None:
- trackLength = subtitleTracks[track][1]
- # Minimum title length is 16 minutes, and
- # maximum is 110 minutes.
- if trackLength > 1000 and trackLength < 6600:
- discTitles.append( track )
- print vobFile, ":"
- for track in discTitles:
- subtitleTrack = subtitleTracks[ track ][0]
- print track, subtitleTrack, subtitleTracks[track][1]
- subprocess.Popen(['./handbrake-track.sh',
- vobFile, str(track),
- str(subtitleTrack)]
- ).communicate()
Add Comment
Please, Sign In to add comment