create_test_videofont.py: make the pipelines more configurable
[vidi-player.git] / create_test_videofont.py
1 #!/usr/bin/env python3
2
3 import sys
4 import os
5 import vidi
6
7 #FONT_DESC = "Andale Mono, 72"
8 FONT_DESC = "Mono, 72"
9
10 SAMPLE_LENGTH_SECONDS = 4
11
12 VIDEO_FRAMERATE = 25
13 VIDEO_BUFFERS = SAMPLE_LENGTH_SECONDS * VIDEO_FRAMERATE
14
15 AUDIO_SAMPLERATE = 48000
16 AUDIO_SAMPLESPERBUFFER = 1024
17 AUDIO_BUFFERS = round(SAMPLE_LENGTH_SECONDS * 48000 / 1024)
18
19 LIVE_PIPELINE_TEMPLATE = """
20   videotestsrc num-buffers=%d pattern=black ! video/x-raw,framerate=%d/1 ! \
21           textoverlay valignment=center halignment=center font-desc="%s" text="{0}" ! \
22           autovideosink \
23   audiotestsrc num-buffers=%d samplesperbuffer=%d freq={1:f} ! audio/x-raw,rate=%d ! \
24           autoaudiosink
25 """ % (VIDEO_BUFFERS, VIDEO_FRAMERATE, FONT_DESC, AUDIO_BUFFERS,
26        AUDIO_SAMPLESPERBUFFER, AUDIO_SAMPLERATE)
27
28 FILE_PIPELINE_TEMPLATE = """
29   webmmux name=mux ! filesink location="{2}/sample_{0}.webm"
30   videotestsrc num-buffers=%d pattern=black ! video/x-raw,framerate=%d/1 ! \
31           textoverlay valignment=center halignment=center font-desc="%s" text="{0}" ! \
32           queue ! vp9enc ! mux.
33   audiotestsrc num-buffers=%d samplesperbuffer=%d freq={1:f} ! audio/x-raw,rate=%d ! \
34           queue ! audioconvert ! vorbisenc quality=0.5 ! queue ! mux.
35 """ % (VIDEO_BUFFERS, VIDEO_FRAMERATE, FONT_DESC, AUDIO_BUFFERS,
36        AUDIO_SAMPLESPERBUFFER, AUDIO_SAMPLERATE)
37
38
39 def create_test_videofont(pipeline_template, notes_range, destination_dir=None):
40     for i, note_number in enumerate(notes_range):
41         note = vidi.SpnNote(note_number)
42
43         print("%2d %s" % (i, note))
44
45         pipeline_string = pipeline_template.format(note.name, note.frequency, destination_dir)
46
47         player = vidi.Player.from_pipeline_string(pipeline_string)
48         error = player.play()
49         if error:
50             break
51
52
53 def usage():
54     print("usage: %s [<videofont_destination_dir>]" %
55           os.path.basename(sys.argv[0]))
56
57
58 def main():
59     if len(sys.argv) > 1 and sys.argv[1] in ["-h", "--help"]:
60         usage()
61         return 0
62
63     notes_range = vidi.PIANO_88_KEYS_RANGE
64
65     if len(sys.argv) > 1:
66         destination_dir = os.path.realpath(sys.argv[1])
67         if os.path.exists(destination_dir):
68             sys.stderr.write("The destination already exists '%s'!\n"
69                              % destination_dir)
70             return 1
71
72         os.mkdir(destination_dir)
73         create_test_videofont(FILE_PIPELINE_TEMPLATE, notes_range,
74                               destination_dir)
75     else:
76         create_test_videofont(LIVE_PIPELINE_TEMPLATE, notes_range)
77
78
79 if __name__ == "__main__":
80     sys.exit(main())