README.md: fix a couple of typos
[vidi-player.git] / vidi-timeline.py
1 #!/usr/bin/env python3
2 #
3 # vidi-timeline - generate GStreamer Editing Services timelines from midi
4 #
5 # Copyright (C) 2016  Antonio Ospite <ao2@ao2.it>
6 #
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.
11 #
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.
16 #
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/>.
19
20 import os
21 import sys
22 import mido
23 import vidi
24
25 # TODO: turn these into command line options
26 ADD_REST_BACKGROUND = True
27 ADD_TITLE = True
28 TITLE_TEXT = None
29 TITLE_DURATION = 5
30 TITLE_CREDITS = "Created with vidi-timeline\nhttps://git.ao2.it/vidi-player.git"
31 CANVAS_SIZE = (640, 480)
32
33
34 def timeline_from_midi(midi_file, canvas_size, video_font_path):
35     timeline = vidi.Timeline(canvas_size)
36
37     if ADD_TITLE:
38         title = TITLE_TEXT
39         if not title:
40             title = os.path.splitext(os.path.basename(midi_file.filename))[0]
41             title += "\n\n"
42             title += TITLE_CREDITS
43
44         timeline.add_title_clip(title, 0, TITLE_DURATION)
45
46         elapsed_time = start_time = TITLE_DURATION
47     else:
48         elapsed_time = start_time = 0
49
50     for msg in midi_file:
51         elapsed_time += msg.time
52         if vidi.is_note_on(msg) and msg.channel == 0:
53             start_time = elapsed_time
54         elif vidi.is_note_off(msg) and msg.channel == 0:
55             note = vidi.MidiNote(msg.note)
56             duration = elapsed_time - start_time
57             print("Note name: %3s start_time: %f duration: %f" %
58                   (note.name, start_time, duration))
59
60             video_sample_path = "%s/sample_%s.webm" % (video_font_path, note.name)
61
62             timeline.add_clip(video_sample_path, start_time, duration)
63
64     if ADD_REST_BACKGROUND:
65         rest_sample_path = "%s/sample_rest.png" % video_font_path
66         timeline.add_layer_clip(rest_sample_path, 0, elapsed_time)
67
68     return timeline
69
70
71 def usage():
72     print("usage: %s <midi_file> <videofont_directory> [<destination_file>]"
73           % os.path.basename(sys.argv[0]))
74
75
76 def main():
77     if len(sys.argv) > 1 and sys.argv[1] in ["-h", "--help"]:
78         usage()
79         return 0
80
81     if len(sys.argv) < 3:
82         usage()
83         return 1
84
85     if not os.path.isdir(sys.argv[2]):
86         sys.stderr.write("The second argument must be the path of the videofont directory\n")
87         usage()
88         return 1
89
90     if len(sys.argv) > 3 and os.path.exists(sys.argv[3]):
91         sys.stderr.write("File '%s' exists, exiting!\n" % sys.argv[3])
92         return 1
93
94     midi_file = mido.MidiFile(sys.argv[1])
95
96     overlapping_notes = vidi.check_overlapping_notes(midi_file)
97     if overlapping_notes:
98         sys.stderr.write("Sorry, supporting only midi file with no overlapping notes on channel 0\n")
99         return 1
100
101     video_font_path = os.path.realpath(sys.argv[2])
102
103     timeline = timeline_from_midi(midi_file, CANVAS_SIZE, video_font_path)
104
105     if len(sys.argv) > 3:
106         timeline.save(sys.argv[3])
107     else:
108         try:
109             timeline.play()
110         except KeyboardInterrupt:
111             timeline.stop()
112             return 1
113
114
115 if __name__ == "__main__":
116     sys.exit(main())