Spaces:
Sleeping
Sleeping
Upload myinfer_latest.py
Browse files31 unmodified lines323334353637383940206 unmodified lines24724824925025125225325425525625725825926026126226326426526626726826927027127227331 unmodified linesimport iofrom space import ensure_model_in_weights_dir,upload_to_doimport boto3from moviepy.editor import *import os206 unmodified linesdef merge_audio_image(mp3_path, image_path, output_dir, unique_id):# Load the imageimage_clip = ImageClip(image_path)# Load the audioaudio_clip = AudioFileClip(mp3_path)# Set the duration of the image clip to match the audio durationimage_clip = image_clip.set_duration(audio_clip.duration)# Resize the image clip to Instagram's square dimensions (1080x1080)#image_clip = image_clip.resize((1080, 1080))image_clip = image_clip.resize((1080, 1080))# Set the audio to the image clipfinal_clip = image_clip.set_audio(audio_clip)# Generate output file pathoutput_path = os.path.join(output_dir, f"{unique_id}.mp4")# Write the output video filefinal_clip.write_videofile(output_path, codec='libx264', audio_codec='aac')return output_path31 unmodified lines323334353637383940206 unmodified lines24724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627731 unmodified linesimport iofrom space import ensure_model_in_weights_dir,upload_to_doimport boto3import osimport ffmpegimport os206 unmodified linesdef merge_audio_image(mp3_path, image_path, output_dir, unique_id):# Generate output file pathoutput_path = os.path.join(output_dir, f"{unique_id}.mp4")# Ensure the image file existsif not os.path.isfile(image_path):raise FileNotFoundError(f"Image file not found: {image_path}")# Ensure the audio file existsif not os.path.isfile(mp3_path):raise FileNotFoundError(f"Audio file not found: {mp3_path}")# Get the duration of the audio fileprobe = ffmpeg.probe(mp3_path)audio_duration = float(probe['format']['duration'])# Create the ffmpeg command to combine image and audio into a videoinput_image = ffmpeg.input(image_path, loop=1, t=audio_duration)input_audio = ffmpeg.input(mp3_path)# Apply scale and pad filters to the imagevideo_stream = input_image.filter('scale', size='1080x1080', force_original_aspect_ratio='decrease')\.filter('pad', 1080, 1080, -1, -1)# Combine image and audio into a videoffmpeg.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