create_test_videofont.py: generate the PNG rest sample
[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 PNG_REST_SAMPLE_TEMPLATE = """
39   videotestsrc num-buffers=1 pattern=black ! \
40           textoverlay valignment=center halignment=center font-desc="%s" text="rest" ! \
41           pngenc ! filesink location="{0}/sample_rest.png"
42 """ % FONT_DESC
43
44
45 def create_test_videofont(pipeline_template, notes_range, destination_dir=None):
46     print("Silence")
47     pipeline_string = pipeline_template.format("rest", 0, destination_dir)
48     player = vidi.Player.from_pipeline_string(pipeline_string)
49     player.play()
50
51     for i, note_number in enumerate(notes_range):
52         note = vidi.SpnNote(note_number)
53
54         print("%2d %s" % (i, note))
55
56         pipeline_string = pipeline_template.format(note.name, note.frequency, destination_dir)
57
58         player = vidi.Player.from_pipeline_string(pipeline_string)
59         error = player.play()
60         if error:
61             break
62
63
64 def usage():
65     print("usage: %s [<videofont_destination_dir>]" %
66           os.path.basename(sys.argv[0]))
67
68
69 def main():
70     if len(sys.argv) > 1 and sys.argv[1] in ["-h", "--help"]:
71         usage()
72         return 0
73
74     notes_range = vidi.PIANO_88_KEYS_RANGE
75
76     if len(sys.argv) > 1:
77         destination_dir = os.path.realpath(sys.argv[1])
78         if os.path.exists(destination_dir):
79             sys.stderr.write("The destination already exists '%s'!\n"
80                              % destination_dir)
81             return 1
82
83         os.mkdir(destination_dir)
84         create_test_videofont(FILE_PIPELINE_TEMPLATE, notes_range,
85                               destination_dir)
86
87         pipeline_string = PNG_REST_SAMPLE_TEMPLATE.format(destination_dir)
88         player = vidi.Player.from_pipeline_string(pipeline_string)
89         player.play()
90     else:
91         create_test_videofont(LIVE_PIPELINE_TEMPLATE, notes_range)
92
93
94 if __name__ == "__main__":
95     sys.exit(main())