vidi-player.py: check that the output file exists before creating the pipeline
[vidi-player.git] / vidi-player.py
1 #!/usr/bin/env python3
2 #
3 # vidi-player - 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
26 def is_note(msg):
27     return msg.type == 'note_on' or msg.type == 'note_off'
28
29
30 def is_note_on(msg):
31     return msg.type == 'note_on' and msg.velocity > 0
32
33
34 def is_note_off(msg):
35     return ((msg.type == 'note_on' and msg.velocity == 0) or
36             (msg.type == 'note_off'))
37
38
39 def check_overlapping_notes(midi_file):
40     previous_note_on = False
41     for msg in midi_file:
42         if is_note_on(msg) and msg.channel == 0:
43             if previous_note_on:
44                 return True
45
46             previous_note_on = True
47         elif is_note_off(msg) and msg.channel == 0:
48             previous_note_on = False
49
50     return False
51
52
53 def timeline_from_midi(midi_file, video_font_path):
54     timeline = vidi.Timeline()
55
56     elapsed_time = 0
57     start_time = 0
58     for msg in midi_file:
59         elapsed_time += msg.time
60         if is_note_on(msg) and msg.channel == 0:
61             start_time = elapsed_time
62         elif is_note_off(msg) and msg.channel == 0:
63             note = vidi.MidiNote(msg.note)
64             duration = elapsed_time - start_time
65             print("Note name: %s start_time: %f duration: %f" %
66                   (note.name, start_time, duration))
67
68             video_sample_path = "%s/sample_%s.mkv" % (video_font_path, note.name)
69
70             timeline.add_clip(video_sample_path, start_time, duration)
71
72     return timeline
73
74
75 def usage():
76     print("usage: %s <midi_file> <videofont_directory> [<destination_file>]"
77           % os.path.basename(sys.argv[0]))
78
79
80 def main():
81     if len(sys.argv) > 1 and sys.argv[1] in ["-h", "--help"]:
82         usage()
83         return 0
84
85     if len(sys.argv) < 3:
86         usage()
87         return 1
88
89     midi_file = mido.MidiFile(sys.argv[1])
90
91     overlapping_notes = check_overlapping_notes(midi_file)
92     if overlapping_notes:
93         sys.stderr.write("Sorry, supporting only midi file with no overlapping notes on channel 0\n")
94         return 1
95
96     if not os.path.isdir(sys.argv[2]):
97         sys.stderr.write("The second argument must be the path of the videofont directory\n")
98         usage()
99         return 1
100
101     if len(sys.argv) > 3 and os.path.exists(sys.argv[3]):
102         sys.stderr.write("File '%s' exists, exiting!\n" % sys.argv[3])
103         return 1
104
105     video_font_path = os.path.realpath(sys.argv[2])
106
107     timeline = timeline_from_midi(midi_file, video_font_path)
108
109     if len(sys.argv) > 3:
110         timeline.save(sys.argv[3])
111     else:
112         try:
113             timeline.play()
114         except KeyboardInterrupt:
115             timeline.stop()
116             return 1
117
118
119 if __name__ == "__main__":
120     sys.exit(main())