acde8ab6fbe8a31db7339cf6ce9bd0ef8b1c9272
[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     print("Silence")
41     pipeline_string = pipeline_template.format("rest", 0, destination_dir)
42     player = vidi.Player.from_pipeline_string(pipeline_string)
43     player.play()
44
45     for i, note_number in enumerate(notes_range):
46         note = vidi.SpnNote(note_number)
47
48         print("%2d %s" % (i, note))
49
50         pipeline_string = pipeline_template.format(note.name, note.frequency, destination_dir)
51
52         player = vidi.Player.from_pipeline_string(pipeline_string)
53         error = player.play()
54         if error:
55             break
56
57
58 def usage():
59     print("usage: %s [<videofont_destination_dir>]" %
60           os.path.basename(sys.argv[0]))
61
62
63 def main():
64     if len(sys.argv) > 1 and sys.argv[1] in ["-h", "--help"]:
65         usage()
66         return 0
67
68     notes_range = vidi.PIANO_88_KEYS_RANGE
69
70     if len(sys.argv) > 1:
71         destination_dir = os.path.realpath(sys.argv[1])
72         if os.path.exists(destination_dir):
73             sys.stderr.write("The destination already exists '%s'!\n"
74                              % destination_dir)
75             return 1
76
77         os.mkdir(destination_dir)
78         create_test_videofont(FILE_PIPELINE_TEMPLATE, notes_range,
79                               destination_dir)
80     else:
81         create_test_videofont(LIVE_PIPELINE_TEMPLATE, notes_range)
82
83
84 if __name__ == "__main__":
85     sys.exit(main())