Spaces:
Sleeping
Sleeping
Upload img_audio_merge.py
Browse files- img_audio_merge.py +19 -18
img_audio_merge.py
CHANGED
|
@@ -1,27 +1,28 @@
|
|
| 1 |
-
|
| 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 |
-
#
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
return output_path
|
| 27 |
|
|
|
|
| 1 |
+
import ffmpeg
|
| 2 |
import os
|
| 3 |
|
| 4 |
def merge_audio_image(mp3_path, image_path, output_dir, unique_id):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Generate output file path
|
| 6 |
output_path = os.path.join(output_dir, f"{unique_id}.mp4")
|
| 7 |
|
| 8 |
+
# Ensure the image file exists
|
| 9 |
+
if not os.path.isfile(image_path):
|
| 10 |
+
raise FileNotFoundError(f"Image file not found: {image_path}")
|
| 11 |
+
|
| 12 |
+
# Ensure the audio file exists
|
| 13 |
+
if not os.path.isfile(mp3_path):
|
| 14 |
+
raise FileNotFoundError(f"Audio file not found: {mp3_path}")
|
| 15 |
+
|
| 16 |
+
# Create the ffmpeg command to combine image and audio into a video
|
| 17 |
+
(
|
| 18 |
+
ffmpeg
|
| 19 |
+
.input(image_path)
|
| 20 |
+
.filter('scale', size='1080x1080', force_original_aspect_ratio='decrease')
|
| 21 |
+
.pad('1080', '1080', -1, -1)
|
| 22 |
+
.input(mp3_path)
|
| 23 |
+
.output(output_path, vcodec='libx264', acodec='aac', strict='experimental', pix_fmt='yuv420p', shortest=None)
|
| 24 |
+
.run()
|
| 25 |
+
)
|
| 26 |
|
| 27 |
return output_path
|
| 28 |
|