vidi/Player.py: move the stop() method after the play() method
[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 # 48000 samples per seconds, at 1024 samples per buffer
13 NUM_AUDIO_BUFFERS = round(SAMPLE_LENGTH_SECONDS * 48000 / 1024)
14
15 LIVE_PIPELINE_TEMPLATE = """
16   videotestsrc num-buffers=1 pattern=black ! \
17           textoverlay valignment=center halignment=center font-desc="%s" text="{0}" ! \
18           autovideosink \
19   audiotestsrc num-buffers=%d freq={1:f} ! audio/x-raw,rate=48000 ! \
20           autoaudiosink
21 """ % (FONT_DESC, NUM_AUDIO_BUFFERS)
22
23 FILE_PIPELINE_TEMPLATE = """
24   matroskamux name=mux ! filesink location="{2}/sample_{0}.mkv"
25   videotestsrc num-buffers=1 pattern=black ! \
26           textoverlay valignment=center halignment=center font-desc="%s" text="{0}" ! \
27           queue ! schroenc rate-control=3 ! mux.
28   audiotestsrc num-buffers=%d freq={1:f} ! audio/x-raw,rate=48000 ! \
29           queue ! audioconvert ! vorbisenc quality=0.5 ! queue ! mux.
30 """ % (FONT_DESC, NUM_AUDIO_BUFFERS)
31
32
33 def create_test_videofont(pipeline_template, notes_range, destination_dir=None):
34     for i, note_number in enumerate(notes_range):
35         note = vidi.SpnNote(note_number)
36
37         print("%2d %s" % (i, note))
38
39         pipeline_string = pipeline_template.format(note.name, note.frequency, destination_dir)
40
41         player = vidi.Player.from_pipeline_string(pipeline_string)
42         error = player.play()
43         if error:
44             break
45
46
47 def usage():
48     print("usage: %s [<videofont_destination_dir>]" %
49           os.path.basename(sys.argv[0]))
50
51
52 def main():
53     if len(sys.argv) > 1 and sys.argv[1] in ["-h", "--help"]:
54         usage()
55         return 0
56
57     notes_range = vidi.PIANO_88_KEYS_RANGE
58
59     if len(sys.argv) > 1:
60         destination_dir = os.path.realpath(sys.argv[1])
61         if os.path.exists(destination_dir):
62             sys.stderr.write("The destination already exists '%s'!\n"
63                              % destination_dir)
64             return 1
65
66         os.mkdir(destination_dir)
67         create_test_videofont(FILE_PIPELINE_TEMPLATE, notes_range,
68                               destination_dir)
69     else:
70         create_test_videofont(LIVE_PIPELINE_TEMPLATE, notes_range)
71
72
73 if __name__ == "__main__":
74     sys.exit(main())