Spaces:
Runtime error
Runtime error
Update Moviepy
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455from moviepy.editor import *current_video_path=r"C:\Users\ASUS\Downloads\Introducing the New Range Rover Sport-gWS1wLfiDVU-480pp-1703424984.mp4"current_video=VideoFileClip(current_video_path) #location of the uploaded videocurrent_audio_path="current_audio.mp3"if current_video.audio is not None:current_video.audio.write_audiofile(current_audio_path) #location of the audio clipprint("Audio file has been extracted from the video")else:print("No audio found in the video.")current_audio=AudioFileClip(current_audio_path) #location of the whole audio clip from the uloaded video#define the segements for the audio clipsstart_explosion_time=3 # Define the start time of the explosion partend_explosion_time = 6 # Define the end time of the explosion part#extract audio segementsbeginning_audio_clip=current_audio.subclip(0,start_explosion_time) #0 = start, end value will be the beginning of the explotion partend_audio_clip=current_audio.subclip(end_explosion_time,) #start = end of the explotion, end = end of the audio#haptic_audio_clip=current_audio.subclip(start_explosion_time,end_explosion_time) #start = start of the explotion, end = end of the explotionhaptic_audio_path='https://phonebrrdemonstration2.blob.core.windows.net/audio3secondsmp3/3_second_explosion_00001.mp3'haptic_audio=AudioFileClip(haptic_audio_path)haptic_audio_clip=haptic_audio.subclip() #start = start of the explotion, end = end of the explotion#concatenate audio clips to create enhanced audiofinal_audio=concatenate_audioclips([beginning_audio_clip,haptic_audio_clip,end_audio_clip]) #enhanced audio#save the enhanced audiofinal_audio_path="output.mp3"final_audio.write_audiofile(final_audio_path) #enhanced audio "name"print("Enhanced audio has been created")#create a video without audioextracted_video=current_video.without_audio()#save the video without audioextracted_video_path="output.mp4"extracted_video.write_videofile(extracted_video_path,fps=60)print("Video without audio has been created")#combine the final video without the enhanced audiofinal_video=extracted_video.set_audio(final_audio)#save the final videofinal_video_path="final_video.mp4"final_video.write_videofile(final_video_path)print("final video has been created")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106from moviepy.editor import *import jsoncurrent_video_path = r"C:\Users\ASUS\Desktop\UoW\2ND YEAR\SDGP\Test Video\test_video.mp4"current_video = VideoFileClip(current_video_path) # location of the uploaded videooutput_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# Convert JSON string to Python dictionarydata = json.loads(output_query_response)# creating an empty array for the resultsresult = []# iterate over every explosion occurrence and find start, end, best values using the ml algorithmfor i in data["value"]:result.append([i["start"], i["end"], i["best"]])# update start_explosion_time and end_explosion_time for each explosion occurrencefor explosion in result:best_time = explosion[2]best_time_seconds = sum(x * int(t) for x, t in zip([3600, 60, 1], best_time.split(":")))start_explosion_time_seconds = best_time_seconds - 1end_explosion_time_seconds = best_time_seconds + 2explosion[0] = "{:02d}:{:02d}:{:02d}".format(start_explosion_time_seconds // 3600,(start_explosion_time_seconds % 3600 // 60),start_explosion_time_seconds % 60)explosion[1] = "{:02d}:{:02d}:{:02d}".format(end_explosion_time_seconds // 3600,(end_explosion_time_seconds % 3600 // 60),end_explosion_time_seconds % 60)# Extracting audio from the videocurrent_audio_path = "current_audio.mp3"if current_video.audio is not None:current_video.audio.write_audiofile(current_audio_path) # location of the audio clipprint("Audio file has been extracted from the video")else:print("No audio found in the video.")# Loading the whole audio clip from the uploaded videocurrent_audio = AudioFileClip(current_audio_path)# define the segments for the audio clipsfinal_audio_segments = []haptic_audio_path = 'https://phonebrrdemonstration2.blob.core.windows.net/audio3secondsmp3/3_second_explosion_00001.mp3'haptic_audio = AudioFileClip(haptic_audio_path)# Iterate through each explosion occurrence and create audio segmentsfor explosion in result:start_explosion_time = sum(x * int(t) for x, t in zip([3600, 60, 1], explosion[0].split(":")))end_explosion_time = sum(x * int(t) for x, t in zip([3600, 60, 1], explosion[1].split(":")))beginning_audio_clip = current_audio.subclip(0, start_explosion_time)end_audio_clip = current_audio.subclip(end_explosion_time)# Adjust the duration of the haptic audio to match the duration between start and end explosion timeshaptic_audio_duration = end_explosion_time - start_explosion_timehaptic_audio_clip = haptic_audio.subclip(0, haptic_audio_duration)final_audio = concatenate_audioclips([beginning_audio_clip, haptic_audio_clip, end_audio_clip])final_audio_segments.append(final_audio)# concatenate final audio segmentsfinal_audio = concatenate_audioclips(final_audio_segments)# Match the audio duration with the video durationfinal_audio = final_audio.set_duration(current_video.duration)# Save the enhanced audiofinal_audio_path = "output.mp3"final_audio.write_audiofile(final_audio_path)print("Enhanced audio has been created")# create a video without audioextracted_video = current_video.without_audio()# save the video without audioextracted_video_path = "output.mp4"extracted_video.write_videofile(extracted_video_path, fps=60)print("Video without audio has been created")# combine the final video without the enhanced audiofinal_video = extracted_video.set_audio(final_audio)# save the final videofinal_video_path = "final_video.mp4"final_video.write_videofile(final_video_path)print("Final video has been created")
Moviepy+82-31