File size: 1,647 Bytes
b1a3bc7
8e8779d
 
 
 
 
 
b1a3bc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e8779d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()