Spaces:
Sleeping
Sleeping
Upload myinfer_latest.py
Browse files- myinfer_latest.py +21 -17
myinfer_latest.py
CHANGED
@@ -32,9 +32,9 @@ from flask_dance.contrib.google import make_google_blueprint, google
|
|
32 |
import io
|
33 |
from space import ensure_model_in_weights_dir,upload_to_do
|
34 |
import boto3
|
35 |
-
from moviepy.editor import *
|
36 |
import os
|
37 |
-
|
|
|
38 |
|
39 |
|
40 |
|
@@ -247,27 +247,31 @@ def get_status(audio_id):
|
|
247 |
|
248 |
|
249 |
def merge_audio_image(mp3_path, image_path, output_dir, unique_id):
|
250 |
-
#
|
251 |
-
|
252 |
|
253 |
-
#
|
254 |
-
|
|
|
255 |
|
256 |
-
#
|
257 |
-
|
|
|
258 |
|
259 |
-
#
|
260 |
-
|
261 |
-
|
262 |
|
263 |
-
#
|
264 |
-
|
|
|
265 |
|
266 |
-
#
|
267 |
-
|
|
|
268 |
|
269 |
-
#
|
270 |
-
|
271 |
|
272 |
return output_path
|
273 |
|
|
|
32 |
import io
|
33 |
from space import ensure_model_in_weights_dir,upload_to_do
|
34 |
import boto3
|
|
|
35 |
import os
|
36 |
+
import ffmpeg
|
37 |
+
import os
|
38 |
|
39 |
|
40 |
|
|
|
247 |
|
248 |
|
249 |
def merge_audio_image(mp3_path, image_path, output_dir, unique_id):
|
250 |
+
# Generate output file path
|
251 |
+
output_path = os.path.join(output_dir, f"{unique_id}.mp4")
|
252 |
|
253 |
+
# Ensure the image file exists
|
254 |
+
if not os.path.isfile(image_path):
|
255 |
+
raise FileNotFoundError(f"Image file not found: {image_path}")
|
256 |
|
257 |
+
# Ensure the audio file exists
|
258 |
+
if not os.path.isfile(mp3_path):
|
259 |
+
raise FileNotFoundError(f"Audio file not found: {mp3_path}")
|
260 |
|
261 |
+
# Get the duration of the audio file
|
262 |
+
probe = ffmpeg.probe(mp3_path)
|
263 |
+
audio_duration = float(probe['format']['duration'])
|
264 |
|
265 |
+
# Create the ffmpeg command to combine image and audio into a video
|
266 |
+
input_image = ffmpeg.input(image_path, loop=1, t=audio_duration)
|
267 |
+
input_audio = ffmpeg.input(mp3_path)
|
268 |
|
269 |
+
# Apply scale and pad filters to the image
|
270 |
+
video_stream = input_image.filter('scale', size='1080x1080', force_original_aspect_ratio='decrease')\
|
271 |
+
.filter('pad', 1080, 1080, -1, -1)
|
272 |
|
273 |
+
# Combine image and audio into a video
|
274 |
+
ffmpeg.output(video_stream, input_audio, output_path, vcodec='libx264', acodec='aac', strict='experimental', pix_fmt='yuv420p').run()
|
275 |
|
276 |
return output_path
|
277 |
|