Guest User

Untitled

a guest
May 16th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. ###############################################################################
  4. # process-tv.py Wes Brown, (c) 2009
  5. # Takes for input a directory containing DVD rips. Rips are assumed to be in
  6. # the format of SeriesName-S1-D1. Cannot have space in rip names.
  7. #
  8. # Freely distributable as long as there is attribution.
  9. ###############################################################################
  10.  
  11. import re
  12. import sys
  13. import os
  14. import subprocess
  15.  
  16. # Our directory to scan for DVD rips.
  17. path = sys.argv[1]
  18.  
  19. # This routine scans the titles available on the DVD rip. It looks for
  20. # available English subtitles, and finds the length of each DVD title.
  21. # It will only return titles that have subtitles or captions.
  22.  
  23. def AutodetectTracksSubtitles( vobFile ):
  24. queryResult = subprocess.Popen(['HandbrakeCLI', '-t', '0', '-i',
  25. vobFile], stdout=subprocess.PIPE,
  26. stderr=subprocess.PIPE ).communicate()
  27. resultLines = queryResult[1].split("\n")
  28.  
  29. returnArray = {}
  30.  
  31. titleNo = []
  32. captionTrack = []
  33. subtitleTrack = []
  34. seconds = []
  35.  
  36. for line in resultLines:
  37. titleQuery = re.findall("\+ title (\d+)", line)
  38. if titleQuery:
  39. # Flush out our current status.
  40. if titleNo:
  41. # Our preferred default is closed captions.
  42. if captionTrack != []:
  43. returnArray[ titleNo ] = \
  44. [ captionTrack, seconds ]
  45.  
  46. # If there are no closed captions, fall back to
  47. # subtitles.
  48. elif subtitleTrack != []:
  49. returnArray[ titleNo ] = \
  50. [ subtitleTrack, seconds ]
  51.  
  52. # If there are no subtitles or closed caption,
  53. # it is probably a junk track. Do not return.
  54. else:
  55. returnArray[ titleNo ] = None
  56.  
  57. # Flush for the next title.
  58. captionTrack = []
  59. subtitleTrack = []
  60. seconds = []
  61.  
  62. titleNo = titleQuery[0]
  63.  
  64. # Find out which subtitle tracc has closed captions.
  65. captionTrack = re.findall("(\d+), Closed Captions \(iso639-2: eng\) \(Text\)", line)
  66. if captionTrack:
  67. captionTrack = captionTrack[0]
  68.  
  69. # Find out which subtitle track has English subtitles.
  70. englishSubtitleLine = re.findall(
  71. "\(iso639-2: eng\)", line )
  72. if englishSubtitleLine:
  73. subtitleTrack = re.findall("\+ (\d+),", line)[0]
  74.  
  75. # Let's figure out how long our title is.
  76. durationLine = re.findall("\+ duration: (\d+):(\d+):(\d+)", line)
  77. if durationLine:
  78. hours, minutes, seconds = durationLine[0]
  79. seconds = int(hours) * 60 * 60 + int(minutes) * 60 + \
  80. int(seconds)
  81.  
  82. # Do a final flush before returning the array.
  83. if titleNo:
  84. if captionTrack != []:
  85. returnArray[ titleNo ] = [ captionTrack, seconds ]
  86. elif subtitleTrack != []:
  87. returnArray[ titleNo ] = [ subtitleTrack, seconds ]
  88. else:
  89. returnArray[ titleNo ] = None
  90.  
  91. return returnArray
  92.  
  93. for filename in os.listdir( path ):
  94. # The assumption is that DVD rips are in directories in the format of:
  95. # SeriesName-S1-D1 - name, season, and disk.
  96. fileInfo = filename.split('-')
  97.  
  98. # We don't want to try to transcode using already transcoded files.
  99. if len(fileInfo) == 3 and not( ".m4v" in filename ):
  100. vobFile = path + '/' + filename
  101. show, season, disk = fileInfo
  102.  
  103. # Get our subtitles and valid titles.
  104. subtitleTracks = AutodetectTracksSubtitles( vobFile )
  105. discTitles = []
  106.  
  107.  
  108. for track in subtitleTracks:
  109. if subtitleTracks[track] != None:
  110. trackLength = subtitleTracks[track][1]
  111.  
  112. # Minimum title length is 16 minutes, and
  113. # maximum is 110 minutes.
  114. if trackLength > 1000 and trackLength < 6600:
  115. discTitles.append( track )
  116.  
  117. print vobFile, ":"
  118. for track in discTitles:
  119. subtitleTrack = subtitleTracks[ track ][0]
  120. print track, subtitleTrack, subtitleTracks[track][1]
  121. subprocess.Popen(['./handbrake-track.sh',
  122. vobFile, str(track),
  123. str(subtitleTrack)]
  124. ).communicate()
Add Comment
Please, Sign In to add comment