From: Antonio Ospite Date: Thu, 30 Aug 2018 13:37:08 +0000 (+0200) Subject: Add ges-multi-split.py X-Git-Url: https://git.ao2.it/experiments/gstreamer.git/commitdiff_plain/d34a68551efa6fc231dd767ba3ef6bd005c5a5ce?ds=sidebyside;hp=48c3acab5f86b4aab3f7a6d7decc5f3294bced54 Add ges-multi-split.py --- diff --git a/python/ges-multi-split.py b/python/ges-multi-split.py new file mode 100755 index 0000000..dbc3641 --- /dev/null +++ b/python/ges-multi-split.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +# +# ges-multi-cut - generate commands to split a video file into multiple clips +# +# Copyright (C) 2016 Antonio Ospite +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import os +import sys + +CLIP_FILENAME_TEMPLATE = "sample_%s.webm" +CLIP_FORMAT = "video/webm:video/x-vp8:audio/x-vorbis" + + +def timeformat_to_seconds(timeformat): + """Convert a time string in the format HH:MM:SS.dd to seconds""" + multipliers = [3600, 60, 1] + return sum([mult*val for mult, val in + zip([float(t) for t in timeformat.split(':')], multipliers)]) + + +def parse_samples_list(samples_list_filename): + """Parse a file in a custom format. + Each line in the file has this format: + + SAMPLE_NAME HH:MM:SS.ddd HH:MM:SS.ddd + + where sample name is a string and the two time fields are the start time of the + sample and the end time of the sample. + + Returns a list where each item is a tuple: + (sample_name, start_time, end_time) + """ + samples = [] + + with open(samples_list_filename, "r") as samples_list_file: + for row in samples_list_file: + sample_name, start_timeformat, end_timeformat = row.split() + + start_time = timeformat_to_seconds(start_timeformat) + end_time = timeformat_to_seconds(end_timeformat) + + samples.append((sample_name, start_time, end_time)) + + return samples + + +def usage(): + print("usage: %s " + % os.path.basename(sys.argv[0])) + + +def main(): + if len(sys.argv) < 3: + usage() + return 1 + + master_file = sys.argv[1] + samples_list_filename = sys.argv[2] + + samples = parse_samples_list(samples_list_filename) + for sample_name, start_time, end_time in samples: + duration = round(end_time - start_time, 2) + clip_filename = CLIP_FILENAME_TEMPLATE % sample_name + print("ges-launch-1.0 +clip \"%s\" inpoint=%s duration=%s -o \"%s\" --format=\"%s\"" % + (master_file, start_time, duration, clip_filename, CLIP_FORMAT)) + + +if __name__ == "__main__": + sys.exit(main())