def main(): "Command-line function." from optparse import OptionParser parser = OptionParser() parser.add_option("--id", dest="track_id", type="int", help="set new trackID") parser.add_option("--dur", dest="dur", type="int", help="set new default_sample_duration") (options, args) = parser.parse_args() if len(args) < 1: parser.error("Wrong number of arguments") sys.exit(1) for file_name in args: try: make_backup(file_name) except BackupError: print("Backup-file already exists. Skipping file %s" % file_name) continue stpp_cleaner = STPPFixerFilter(file_name, new_track_id=options.track_id, new_default_sample_duration=options.dur) print "Processing %s" % file_name output = stpp_cleaner.filter_top_boxes() os.rename(file_name, bup_name) with open(file_name, "wb") as ofh: ofh.write(output)
def fix_sidx_ranges_in_mpd(self, mpd_file, sidx_ranges): "Fix sidx ranges MPD file." output = StringIO() with open(mpd_file, 'rb') as ifh: self._fix_sidx_ranges(ifh, output, sidx_ranges) try: make_backup(mpd_file) except BackupError: print("Backupfile already exists. Will not overwrite %s" % mpd_file) return with open(mpd_file, 'wb') as ofh: ofh.write(output.getvalue())
def resegment(self): "Resegment the track with new duration." self.input_parser = TrackDataExtractor(self.input_file, self.verbose) ip = self.input_parser ip.filter_top_boxes() if len(ip.input_segments) == 0: raise ValueError("No fragments found in input file. Progressive " "file?") timescale = ip.track_timescale if self.verbose: for i, segment in enumerate(ip.input_segments): print("Input segment %d: dur=%d" % (i + 1, segment['duration'])) segment_info = self._map_samples_to_new_segments() self.track_id = ip.track_id output_segments = [] segment_sizes = [] for i, seg_info in enumerate(segment_info): output_segment = "" if ip.styp: output_segment += ip.styp output_segment += self._generate_moof(i+1, seg_info) output_segment += ip.construct_new_mdat(seg_info) output_segments.append(output_segment) segment_sizes.append(len(output_segment)) if self.output_file: if self.output_file == self.input_file: try: make_backup(self.input_file) except BackupError: print("Backup file for %s already exists" % self.input_file) return with open(self.output_file, "wb") as ofh: input_header_end = self.input_parser.find_header_end() ofh.write(ip.data[:input_header_end]) if not self.skip_sidx: sidx = self._generate_sidx(segment_info, segment_sizes, timescale) ofh.write(sidx) sidx_start = input_header_end self.sidx_range = "%d-%d" % (sidx_start, sidx_start + len(sidx) - 1) for output_segment in output_segments: ofh.write(output_segment)
def process_files(files): for f in files: f_backup = f + '_bup' if os.path.exists(f_backup): print("%s already exists, will not process %s" % (f_backup, f)) continue sto = ShiftCompositionTimeOffset(f) output = sto.filter_top_boxes() input = open(f, 'rb').read() if output != input: assert len(output) == len(input) print("Change in file %s. Make backup %s" % (f, f_backup)) try: make_backup(f) except BackupError: print("Cannot make backup for %s. Skipping it" % f) return with open(f, 'wb') as ofh: ofh.write(output)
def add_subtitles(mpd_file, subtitle_files): "Add subtitles to a DASH manifest file." with open(mpd_file, 'r') as ifh: mpd_content = ifh.read() as_end = r"\</AdaptationSet\>[\r\n]*" # Open file, parse manifest, find proper place to add Adaptaiton set sub_xml = ''.join(sub_file.adaptation_set for sub_file in subtitle_files) as_end_mobj = re.search(as_end, mpd_content) if as_end_mobj is None: raise ValueError('Cound not find AdaptationSet in %s' % mpd_file) end_pos = as_end_mobj.end() output_mpd = mpd_content[:end_pos] + sub_xml + mpd_content[end_pos:] try: make_backup(mpd_file) except BackupError: print("Backup-file already exists. Skipping file %s" % mpd_file) with open(mpd_file, 'w') as ofh: ofh.write(output_mpd)