def from_string(cls, source): match = cls.RE_ITEM.match(source) if not match: raise InvalidItem(source) data = dict(match.groupdict()) for group in ('start', 'end'): data[group] = SubRipTime.from_string(data[group]) return cls(**data)
def get_video_length(path): try: process = subprocess.Popen(['avconv', '-i', path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, stderr = process.communicate() matches = re.search(r"Duration:\s{1}(?P<hours>\d+?):(?P<minutes>\d+?):(?P<seconds>\d+\.\d+?),", stdout, re.DOTALL).groupdict() seconds = Decimal(matches['seconds']).quantize(Decimal('1.000')) secsAndMilli = str(seconds).partition('.') time = matches['hours'].zfill(2)+':'+matches['minutes'].zfill(2)+':'+secsAndMilli[0].zfill(2)+','+secsAndMilli[2].zfill(3) return SubRipTime.from_string(time) except AttributeError: print "No such file: ",path sys.exit(1)
parser.add_argument('output', help='The file where the fixed subs should be saved') parser.add_argument('-i', '--inputVideo1', nargs=1, help='The first video file of the two to combine.') parser.add_argument('-o', '--offset2', nargs=1, help='Offset of second input file from end of first in format HH:MM:SS,mmm') parser.add_argument('-e', '--encoding', nargs=1, help='Encoding of the subtitle files, must be the same. Default is utf-8.') args = parser.parse_args() if not args.inputVideo1 and not args.offset2: parser.error('One of -i/--inputVideo1 or -o/--offset2 are required') sys.exit(1) zeroTime = "00:00:00,000" if args.inputVideo1: length1Time = get_video_length(args.inputVideo1[0]) offset2Time = SubRipTime.from_string(zeroTime) inVid1 = args.inputVideo1[0] if args.offset2: offset2Time = SubRipTime.from_string(args.offset2[0]) length1Time = SubRipTime.from_string(zeroTime) offset2 = args.offset2[0] inSubName1 = args.input1 inSubName2 = args.input2 outSubName = args.output if args.encoding: encoding = args.encoding[0] else: encoding = args.encoding try: inSub1 = SubRipFile.open(inSubName1,encoding) except AttributeError: