Spaces:
Sleeping
Sleeping
Upload myinfer_latest.py
Browse files31 unmodified lines3233343535373637383940206 unmodified lines24724824925025125025125225325425325425525625625725725825926025926026126126226326426326426526626726826626726927027127226927027327427527627731 unmodified linesimport io
from space import ensure_model_in_weights_dir,upload_to_do
import boto3
from moviepy.editor import *
import os
import ffmpeg
import os
206 unmodified lines
def merge_audio_image(mp3_path, image_path, output_dir, unique_id):
# Load the image
image_clip = ImageClip(image_path)
# Generate output file path
output_path = os.path.join(output_dir, f"{unique_id}.mp4")
# Load the audio
audio_clip = AudioFileClip(mp3_path)
# Ensure the image file exists
if not os.path.isfile(image_path):
raise FileNotFoundError(f"Image file not found: {image_path}")
# Set the duration of the image clip to match the audio duration
image_clip = image_clip.set_duration(audio_clip.duration)
# Ensure the audio file exists
if not os.path.isfile(mp3_path):
raise FileNotFoundError(f"Audio file not found: {mp3_path}")
# Resize the image clip to Instagram's square dimensions (1080x1080)
#image_clip = image_clip.resize((1080, 1080))
image_clip = image_clip.resize((1080, 1080))
# Get the duration of the audio file
probe = ffmpeg.probe(mp3_path)
audio_duration = float(probe['format']['duration'])
# Set the audio to the image clip
final_clip = image_clip.set_audio(audio_clip)
# Create the ffmpeg command to combine image and audio into a video
input_image = ffmpeg.input(image_path, loop=1, t=audio_duration)
input_audio = ffmpeg.input(mp3_path)
# Generate output file path
output_path = os.path.join(output_dir, f"{unique_id}.mp4")
# Apply scale and pad filters to the image
video_stream = input_image.filter('scale', size='1080x1080', force_original_aspect_ratio='decrease')\
.filter('pad', 1080, 1080, -1, -1)
# Write the output video file
final_clip.write_videofile(output_path, codec='libx264', audio_codec='aac')
# Combine image and audio into a video
ffmpeg.output(video_stream, input_audio, output_path, vcodec='libx264', acodec='aac', strict='experimental', pix_fmt='yuv420p').run()
return output_path
myinfer_latest.py+21-17