Spaces:
Sleeping
Sleeping
Upload img_audio_merge.py
Browse files123456789101112131415161718192021222324252627from moviepy.editor import *import osdef 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 (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_path12345678910111213141516171819202122232425262728import ffmpegimport osdef 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}")# Create the ffmpeg command to combine image and audio into a video(ffmpeg.input(image_path).filter('scale', size='1080x1080', force_original_aspect_ratio='decrease').pad('1080', '1080', -1, -1).input(mp3_path).output(output_path, vcodec='libx264', acodec='aac', strict='experimental', pix_fmt='yuv420p', shortest=None).run())return output_path
img_audio_merge.py+19-18