Spaces:
Runtime error
Runtime error
Update Moviepy (#16)
Browse files- Update Moviepy (f6097a4ac225dbbc357527b2d45532af9f4b1c70)
Co-authored-by: Shashika Sellapperuma <Shashika-HF@users.noreply.huggingface.co>
Moviepy
CHANGED
|
@@ -1,55 +1,106 @@
|
|
| 1 |
from moviepy.editor import *
|
|
|
|
| 2 |
|
| 3 |
-
current_video_path=r"C:\Users\ASUS\Downloads\Introducing the New Range Rover Sport-gWS1wLfiDVU-480pp-1703424984.mp4"
|
| 4 |
-
current_video=VideoFileClip(current_video_path) #location of the uploaded video
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
print("Audio file has been extracted from the video")
|
| 10 |
else:
|
| 11 |
print("No audio found in the video.")
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
-
#define the
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
#extract audio segements
|
| 21 |
-
beginning_audio_clip=current_audio.subclip(0,start_explosion_time) #0 = start, end value will be the beginning of the explotion part
|
| 22 |
-
end_audio_clip=current_audio.subclip(end_explosion_time,) #start = end of the explotion, end = end of the audio
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
haptic_audio_clip=haptic_audio.subclip() #start = start of the explotion, end = end of the explotion
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
print("Enhanced audio has been created")
|
| 37 |
|
| 38 |
-
#create a video without audio
|
| 39 |
-
extracted_video=current_video.without_audio()
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
print("Video without audio has been created")
|
| 45 |
|
| 46 |
|
| 47 |
-
#combine the final video without the enhanced audio
|
| 48 |
-
final_video=extracted_video.set_audio(final_audio)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
final_video.write_videofile(final_video_path)
|
| 53 |
-
print("
|
|
|
|
| 54 |
|
| 55 |
|
|
|
|
| 1 |
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 |
|