3 # ges-split-samples - generate commands to split a video file into samples
5 # Copyright (C) 2016 Antonio Ospite <ao2@ao2.it>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 CLIP_FORMAT = "video/webm:video/x-vp8:audio/x-vorbis"
24 CLIP_FILE_EXTENSION = "webm"
25 CLIP_FILENAME_TEMPLATE = "sample_%s.%s"
28 def parse_audacity_labels(samples_list_filename):
29 """Parse labels exported from Audacity.
31 NOTE: Audacity uses the current user locale when exporting the labels, but
32 this way there is no portable way to parse the output, so we just assume
33 here that the C locale was used.
35 Basically run "LANG=C audacity" before exporting the data.
39 with open(samples_list_filename, "r") as samples_list_file:
40 for row in samples_list_file:
41 if row.startswith('#'):
44 start_time, end_time, sample_name = row.split()
46 start_time = float(start_time)
47 end_time = float(end_time)
49 samples.append((sample_name, start_time, end_time))
55 print("usage: %s <video_file> <samples_list_file> <destination_dir>"
56 % os.path.basename(sys.argv[0]))
57 print("sample_list_file is in the format used by Audacity when exporting Label Tracks")
61 if len(sys.argv) > 1 and sys.argv[1] in ["-h", "--help"]:
69 master_file = sys.argv[1]
70 samples_list_filename = sys.argv[2]
71 destination_dir = sys.argv[3]
73 samples = parse_audacity_labels(samples_list_filename)
74 for sample_name, start_time, end_time in samples:
75 duration = round(end_time - start_time, 2)
76 clip_filename = CLIP_FILENAME_TEMPLATE % (sample_name,
78 clip_path = os.path.join(destination_dir, clip_filename)
79 print("ges-launch-1.0 +clip \"%s\" inpoint=%s duration=%s -o \"%s\" --format=\"%s\"" %
80 (master_file, start_time, duration, clip_path, CLIP_FORMAT))
82 if sample_name == "rest":
83 rest_sample_filename = CLIP_FILENAME_TEMPLATE % (sample_name, "png")
84 rest_sample_path = os.path.join(destination_dir,
86 print("gst-launch-1.0 filesrc location=\"%s\" ! decodebin ! videoconvert ! pngenc snapshot=1 ! filesink location=\"%s\"" %
87 (clip_path, rest_sample_path))
90 if __name__ == "__main__":