Advanced-RVC-Inference / img_audio_merge.py
smjain's picture
Upload img_audio_merge.py
b1a3bc7 verified
import ffmpeg
import os
def merge_audio_image(mp3_path, image_path, output_dir, unique_id):
# Generate output file path
output_path = os.path.join(output_dir, f"{unique_id}.mp4")
# Ensure the image file exists
if not os.path.isfile(image_path):
raise FileNotFoundError(f"Image file not found: {image_path}")
# Ensure the audio file exists
if 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
# Example usage:
def main():
# Example paths (adjust as necessary)
processed_audio_storage = {}
unique_id = 'example_id'
output_path1 = '/path/to/processed_audio.mp3' # Replace with actual path
processed_audio_storage[unique_id] = output_path1
mp3_path = processed_audio_storage[unique_id]
image_path = 'singer.jpg' # Ensure this image is in the same directory as this script
output_dir = './' # Or specify a different directory
# Generate MP4 and get the output path
mp4_path = merge_audio_image(mp3_path, image_path, output_dir, unique_id)
print(f"Generated video at: {mp4_path}")
if __name__ == '__main__':
main()