1
1
from midiutil import MIDIFile
2
2
import numpy as np
3
3
import cv2
4
+ import sys
5
+ import argparse
6
+ import random
7
+ from moviepy .video .io .VideoFileClip import VideoFileClip
4
8
5
9
def averageRGB (frame , everyNPixels ):
6
10
"""
@@ -42,37 +46,67 @@ def averageRGBChoice(frame, colorStage, everyNPixels):
42
46
43
47
return averageColor
44
48
45
- cap = cv2 .VideoCapture ('Class_Room_Tour.avi' )
46
- degrees = [] # MIDI note number
47
- counter = 0
49
+ def getNumberNote (musicDuration , tempo ):
50
+ """
51
+ Return the number of notes that the music must have to be of the specified duration
52
+ musicDuration : the duration of the music (in seconds)
53
+ tempo : the number of BMP of the music
54
+ """
55
+ durationInMinute = musicDuration / 60
56
+ return int (tempo * durationInMinute )
57
+
58
+
59
+
60
+ if __name__ == "__main__" :
61
+ # TODO use that to get the first argument as the filename to use
62
+ # # Set an argument parser
63
+ # parser = argparse.ArgumentParser(description='Convert a video file (.avi) to a music.')
64
+ # parser.add_argument('filename', metavar='filename',
65
+ # help='a video file')
66
+
67
+ # args = vars(parser.parse_args())
68
+
69
+ cap = cv2 .VideoCapture ('Class_Room_Tour.avi' )
70
+ degrees = [] # MIDI note number
71
+ counter = 0
72
+
73
+ # while(cap.isOpened()):
74
+ # ret, frame = cap.read()
75
+ # everyNImages = 10
76
+
77
+ # if frame is None:
78
+ # break
79
+ # else:
80
+ # if counter % everyNImages == 0:
81
+ # degrees.append(averageRGB(frame, 100))
82
+ # counter += 1
48
83
49
- while (cap .isOpened ()):
50
- ret , frame = cap .read ()
51
- everyNImages = 10
84
+ # cap.release()
85
+ # cv2.destroyAllWindows()
52
86
53
- if frame is None :
54
- break
55
- else :
56
- if counter % everyNImages == 0 :
57
- degrees . append ( averageRGB ( frame , 100 ))
58
- counter += 1
87
+ track = 0
88
+ channel = 0
89
+ time = 0 # In beats
90
+ duration = 1 # In beats
91
+ tempo = 50 # In BPM
92
+ volume = 100 # 0-127, as per the MIDI standard
59
93
60
- cap .release ()
61
- cv2 .destroyAllWindows ()
94
+ # Get the duration of the video
95
+ clip = VideoFileClip ("Class_Room_Tour.avi" )
96
+ print ( clip .duration )
97
+ # Define the number of note the music must have according to the tempo
98
+ numberOfNote = getNumberNote (clip .duration , tempo )
62
99
63
- track = 0
64
- channel = 0
65
- time = 0 # In beats
66
- duration = 1 # In beats
67
- tempo = 200 # In BPM
68
- volume = 100 # 0-127, as per the MIDI standard
100
+ # Generate the correct number of note to have a music of the desired length
101
+ for i in range (numberOfNote ):
102
+ degrees .append (random .randint (10 , 100 ))
69
103
70
- MyMIDI = MIDIFile (1 ) # One track, defaults to format 1 (tempo track is created
71
- # automatically)
72
- MyMIDI .addTempo (track , time , tempo )
104
+ MyMIDI = MIDIFile (1 ) # One track, defaults to format 1 (tempo track is created
105
+ # automatically)
106
+ MyMIDI .addTempo (track , time , tempo )
73
107
74
- for i , pitch in enumerate (degrees ):
75
- MyMIDI .addNote (track , channel , pitch , time + i , duration , volume )
108
+ for i , pitch in enumerate (degrees ):
109
+ MyMIDI .addNote (track , channel , pitch , time + i , duration , volume )
76
110
77
- with open ("major-scale.mid" , "wb" ) as output_file :
78
- MyMIDI .writeFile (output_file )
111
+ with open ("major-scale.mid" , "wb" ) as output_file :
112
+ MyMIDI .writeFile (output_file )
0 commit comments