smjain commited on
Commit
8e8779d
1 Parent(s): 69e8e3e

Upload img_audio_merge.py

Browse files
Files changed (1) hide show
  1. img_audio_merge.py +45 -0
img_audio_merge.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from moviepy.editor import *
2
+ import os
3
+
4
+ def merge_audio_image(mp3_path, image_path, output_dir, unique_id):
5
+ # Load the image
6
+ image_clip = ImageClip(image_path)
7
+
8
+ # Load the audio
9
+ audio_clip = AudioFileClip(mp3_path)
10
+
11
+ # Set the duration of the image clip to match the audio duration
12
+ image_clip = image_clip.set_duration(audio_clip.duration)
13
+
14
+ # Resize the image clip to Instagram's square dimensions (1080, 1080)
15
+ image_clip = image_clip.resize((1080, 1080))
16
+
17
+ # Set the audio to the image clip
18
+ final_clip = image_clip.set_audio(audio_clip)
19
+
20
+ # Generate output file path
21
+ output_path = os.path.join(output_dir, f"{unique_id}.mp4")
22
+
23
+ # Write the output video file
24
+ final_clip.write_videofile(output_path, codec='libx264', audio_codec='aac')
25
+
26
+ return output_path
27
+
28
+ # Example usage:
29
+ def main():
30
+ # Example paths (adjust as necessary)
31
+ processed_audio_storage = {}
32
+ unique_id = 'example_id'
33
+ output_path1 = '/path/to/processed_audio.mp3' # Replace with actual path
34
+ processed_audio_storage[unique_id] = output_path1
35
+
36
+ mp3_path = processed_audio_storage[unique_id]
37
+ image_path = 'singer.jpg' # Ensure this image is in the same directory as this script
38
+ output_dir = './' # Or specify a different directory
39
+
40
+ # Generate MP4 and get the output path
41
+ mp4_path = merge_audio_image(mp3_path, image_path, output_dir, unique_id)
42
+ print(f"Generated video at: {mp4_path}")
43
+
44
+ if __name__ == '__main__':
45
+ main()