HussainLatiff Shashika-HF commited on
Commit
a1df37b
Β·
verified Β·
1 Parent(s): 2a6352c

Update Moviepy (#16)

Browse files

- Update Moviepy (f6097a4ac225dbbc357527b2d45532af9f4b1c70)


Co-authored-by: Shashika Sellapperuma <Shashika-HF@users.noreply.huggingface.co>

Files changed (1) hide show
  1. Moviepy +82 -31
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
- current_audio_path="current_audio.mp3"
7
- if current_video.audio is not None:
8
- current_video.audio.write_audiofile(current_audio_path) #location of the audio clip
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  print("Audio file has been extracted from the video")
10
  else:
11
  print("No audio found in the video.")
12
 
13
- current_audio=AudioFileClip(current_audio_path) #location of the whole audio clip from the uloaded video
 
 
14
 
15
 
16
- #define the segements for the audio clips
17
- start_explosion_time=3 # Define the start time of the explosion part
18
- end_explosion_time = 6 # Define the end time of the explosion part
 
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
- #haptic_audio_clip=current_audio.subclip(start_explosion_time,end_explosion_time) #start = start of the explotion, end = end of the explotion
 
 
 
25
 
26
- haptic_audio_path='https://phonebrrdemonstration2.blob.core.windows.net/audio3secondsmp3/3_second_explosion_00001.mp3'
27
- haptic_audio=AudioFileClip(haptic_audio_path)
28
- haptic_audio_clip=haptic_audio.subclip() #start = start of the explotion, end = end of the explotion
29
 
30
- #concatenate audio clips to create enhanced audio
31
- final_audio=concatenate_audioclips([beginning_audio_clip,haptic_audio_clip,end_audio_clip]) #enhanced audio
 
32
 
33
- #save the enhanced audio
34
- final_audio_path="output.mp3"
35
- final_audio.write_audiofile(final_audio_path) #enhanced audio "name"
 
 
 
 
 
 
 
 
 
 
 
 
36
  print("Enhanced audio has been created")
37
 
38
- #create a video without audio
39
- extracted_video=current_video.without_audio()
40
 
41
- #save the video without audio
42
- extracted_video_path="output.mp4"
43
- extracted_video.write_videofile(extracted_video_path,fps=60)
 
 
 
 
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
- #save the final video
51
- final_video_path="final_video.mp4"
 
52
  final_video.write_videofile(final_video_path)
53
- print("final video has been created")
 
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