Shashika-HF commited on
Commit
2d8bef9
Β·
verified Β·
1 Parent(s): 1fbe333

Update Moviepy

Browse files
Files changed (1) hide show
  1. Moviepy +89 -100
Moviepy CHANGED
@@ -2,105 +2,94 @@ from moviepy.editor import *
2
  import json
3
 
4
 
5
- current_video_path = r"C:\Users\ASUS\Desktop\UoW\2ND YEAR\SDGP\Test Video\test_video.mp4"
6
- current_video = VideoFileClip(current_video_path) # location of the uploaded video
7
- output_query_response = '{"value":[{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:00:16","end":"00:00:26","best":"00:00:21","relevance":0.4005849361419678},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:00:06","end":"00:00:16","best":"00:00:09","relevance":0.38852864503860474},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:42","end":"00:01:58","best":"00:01:43","relevance":0.38718080520629883},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:58","end":"00:02:14","best":"00:02:03","relevance":0.3811851143836975},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:00:42","end":"00:00:52","best":"00:00:42","relevance":0.3765566647052765},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:00:26","end":"00:00:42","best":"00:00:28","relevance":0.3718773126602173},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:08","end":"00:01:24","best":"00:01:10","relevance":0.3707084357738495},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:37","end":"00:01:42","best":"00:01:38","relevance":0.36235538125038147},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:29","end":"00:01:37","best":"00:01:33","relevance":0.3606133460998535},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:03","end":"00:01:08","best":"00:01:04","relevance":0.3513660728931427},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:00:00","end":"00:00:06","best":"00:00:05","relevance":0.3378048241138458}]}' # JSON response
8
-
9
-
10
- # Convert JSON string to Python dictionary
11
- data = json.loads(output_query_response)
12
-
13
-
14
- # creating an empty array for the results
15
- result = []
16
-
17
-
18
- # iterate over every explosion occurrence and find start, end, best values using the ml algorithm
19
- for i in data["value"]:
20
- result.append([i["start"], i["end"], i["best"]])
21
-
22
-
23
- # update start_explosion_time and end_explosion_time for each explosion occurrence
24
- for explosion in result:
25
- best_time = explosion[2]
26
- best_time_seconds = sum(x * int(t) for x, t in zip([3600, 60, 1], best_time.split(":")))
27
- start_explosion_time_seconds = best_time_seconds - 1
28
- end_explosion_time_seconds = best_time_seconds + 2
29
- explosion[0] = "{:02d}:{:02d}:{:02d}".format(start_explosion_time_seconds // 3600,
30
- (start_explosion_time_seconds % 3600 // 60),
31
- start_explosion_time_seconds % 60)
32
- explosion[1] = "{:02d}:{:02d}:{:02d}".format(end_explosion_time_seconds // 3600,
33
- (end_explosion_time_seconds % 3600 // 60),
34
- end_explosion_time_seconds % 60)
35
-
36
-
37
- # Extracting audio from the video
38
- current_audio_path = "current_audio.mp3"
39
- if current_video.audio is not None:
40
- current_video.audio.write_audiofile(current_audio_path) # location of the audio clip
41
- print("Audio file has been extracted from the video")
42
- else:
43
- print("No audio found in the video.")
44
-
45
-
46
- # Loading the whole audio clip from the uploaded video
47
- current_audio = AudioFileClip(current_audio_path)
48
-
49
-
50
- # define the segments for the audio clips
51
- final_audio_segments = []
52
- haptic_audio_path = 'https://phonebrrdemonstration2.blob.core.windows.net/audio3secondsmp3/3_second_explosion_00001.mp3'
53
- haptic_audio = AudioFileClip(haptic_audio_path)
54
-
55
-
56
- # Iterate through each explosion occurrence and create audio segments
57
- for explosion in result:
58
- start_explosion_time = sum(x * int(t) for x, t in zip([3600, 60, 1], explosion[0].split(":")))
59
- end_explosion_time = sum(x * int(t) for x, t in zip([3600, 60, 1], explosion[1].split(":")))
60
-
61
- beginning_audio_clip = current_audio.subclip(0, start_explosion_time)
62
- end_audio_clip = current_audio.subclip(end_explosion_time)
63
-
64
- # Adjust the duration of the haptic audio to match the duration between start and end explosion times
65
- haptic_audio_duration = end_explosion_time - start_explosion_time
66
- haptic_audio_clip = haptic_audio.subclip(0, haptic_audio_duration)
67
-
68
- final_audio = concatenate_audioclips([beginning_audio_clip, haptic_audio_clip, end_audio_clip])
69
- final_audio_segments.append(final_audio)
70
-
71
-
72
- # concatenate final audio segments
73
- final_audio = concatenate_audioclips(final_audio_segments)
74
-
75
-
76
- # Match the audio duration with the video duration
77
- final_audio = final_audio.set_duration(current_video.duration)
78
-
79
-
80
- # Save the enhanced audio
81
- final_audio_path = "output.mp3"
82
- final_audio.write_audiofile(final_audio_path)
83
- print("Enhanced audio has been created")
84
-
85
-
86
- # create a video without audio
87
- extracted_video = current_video.without_audio()
88
-
89
-
90
- # save the video without audio
91
- extracted_video_path = "output.mp4"
92
- extracted_video.write_videofile(extracted_video_path, fps=60)
93
- print("Video without audio has been created")
94
-
95
-
96
- # combine the final video without the enhanced audio
97
- final_video = extracted_video.set_audio(final_audio)
98
-
99
-
100
- # save the final video
101
- final_video_path = "final_video.mp4"
102
- final_video.write_videofile(final_video_path)
103
- print("Final video has been created")
104
-
105
 
106
 
 
2
  import json
3
 
4
 
5
+ def moviepy(current_video_path,output_query_response):
6
+
7
+ # Convert JSON string to Python dictionary
8
+ data = json.loads(output_query_response)
9
+
10
+ # creating an empty array for the results
11
+ result = []
12
+
13
+ # iterate over every explosion occurrence and find start, end, best values using the ml algorithm
14
+ for i in data["value"]:
15
+ result.append([i["start"], i["end"], i["best"]])
16
+
17
+ # update start_explosion_time and end_explosion_time for each explosion occurrence
18
+ for explosion in result:
19
+ best_time = explosion[2]
20
+ best_time_seconds = sum(x * int(t) for x, t in zip([3600, 60, 1], best_time.split(":")))
21
+ start_explosion_time_seconds = best_time_seconds - 1
22
+ end_explosion_time_seconds = best_time_seconds + 2
23
+ explosion[0] = "{:02d}:{:02d}:{:02d}".format(start_explosion_time_seconds // 3600,
24
+ (start_explosion_time_seconds % 3600 // 60),
25
+ start_explosion_time_seconds % 60)
26
+ explosion[1] = "{:02d}:{:02d}:{:02d}".format(end_explosion_time_seconds // 3600,
27
+ (end_explosion_time_seconds % 3600 // 60),
28
+ end_explosion_time_seconds % 60)
29
+
30
+ current_video = VideoFileClip(current_video_path) # location of the uploaded video
31
+
32
+ # Extracting audio from the video
33
+ current_audio_path = "current_audio.mp3"
34
+ if current_video.audio is not None:
35
+ current_video.audio.write_audiofile(current_audio_path) # location of the audio clip
36
+ print("Audio file has been extracted from the video")
37
+ else:
38
+ print("No audio found in the video.")
39
+
40
+ # Loading the whole audio clip from the uploaded video
41
+ current_audio = AudioFileClip(current_audio_path)
42
+
43
+ # define the segments for the audio clips
44
+ final_audio_segments = []
45
+ haptic_audio_path = 'https://audiolibrary.blob.core.windows.net/audiolibrary/3_second_audio.flac'
46
+ haptic_audio = AudioFileClip(haptic_audio_path)
47
+
48
+ # Iterate through each explosion occurrence and create audio segments
49
+ for explosion in result:
50
+ start_explosion_time = sum(x * int(t) for x, t in zip([3600, 60, 1], explosion[0].split(":")))
51
+ end_explosion_time = sum(x * int(t) for x, t in zip([3600, 60, 1], explosion[1].split(":")))
52
+
53
+ beginning_audio_clip = current_audio.subclip(0, start_explosion_time)
54
+ end_audio_clip = current_audio.subclip(end_explosion_time)
55
+
56
+ # Adjust the duration of the haptic audio to match the duration between start and end explosion times
57
+ haptic_audio_duration = end_explosion_time - start_explosion_time
58
+ haptic_audio_clip = haptic_audio.subclip(0, haptic_audio_duration)
59
+
60
+ final_audio = concatenate_audioclips([beginning_audio_clip, haptic_audio_clip, end_audio_clip])
61
+ final_audio_segments.append(final_audio)
62
+
63
+ # concatenate final audio segments
64
+ final_audio = concatenate_audioclips(final_audio_segments)
65
+
66
+ # Match the audio duration with the video duration
67
+ final_audio = final_audio.set_duration(current_video.duration)
68
+
69
+ # Save the enhanced audio
70
+ final_audio_path = "output.mp3"
71
+ final_audio.write_audiofile(final_audio_path)
72
+ print("Enhanced audio has been created")
73
+
74
+ # create a video without audio
75
+ extracted_video = current_video.without_audio()
76
+
77
+ # save the video without audio
78
+ extracted_video_path = "output.mp4"
79
+ extracted_video.write_videofile(extracted_video_path, fps=60)
80
+ print("Video without audio has been created")
81
+
82
+ # combine the final video without the enhanced audio
83
+ final_video = extracted_video.set_audio(final_audio)
84
+
85
+ # save the final video
86
+ final_video_path = "final_video.mp4"
87
+ final_video.write_videofile(final_video_path)
88
+ print("Final video has been created")
89
+
90
+ if __name__ == "__main__":
91
+ current_video_path = r"C:\Users\ASUS\Desktop\UoW\2ND YEAR\SDGP\Test Video\test_video.mp4"
92
+ output_query_response = '{"value":[{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:00:16","end":"00:00:26","best":"00:00:21","relevance":0.4005849361419678},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:00:06","end":"00:00:16","best":"00:00:09","relevance":0.38852864503860474},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:42","end":"00:01:58","best":"00:01:43","relevance":0.38718080520629883},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:58","end":"00:02:14","best":"00:02:03","relevance":0.3811851143836975},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:00:42","end":"00:00:52","best":"00:00:42","relevance":0.3765566647052765},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:00:26","end":"00:00:42","best":"00:00:28","relevance":0.3718773126602173},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:08","end":"00:01:24","best":"00:01:10","relevance":0.3707084357738495},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:37","end":"00:01:42","best":"00:01:38","relevance":0.36235538125038147},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:29","end":"00:01:37","best":"00:01:33","relevance":0.3606133460998535},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:01:03","end":"00:01:08","best":"00:01:04","relevance":0.3513660728931427},{"documentId":"sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D","documentKind":"VideoInterval","start":"00:00:00","end":"00:00:06","best":"00:00:05","relevance":0.3378048241138458}]}' # JSON response
93
+ moviepy(current_video_path,output_query_response)
 
 
 
 
 
 
 
 
 
 
 
94
 
95