Created
April 3, 2022 13:18
-
-
Save oclero/99059674b421a2b9168fb81d930bc093 to your computer and use it in GitHub Desktop.
Convert an audio album to video for Youtube
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import shutil | |
import subprocess | |
import os | |
import sys | |
import sys | |
from pathlib import Path | |
INPUT_AUDIO_DIR = './audio' | |
OUPUT_VIDEO_DIR = './video' | |
audio_files = os.listdir(INPUT_AUDIO_DIR) | |
image_file = './cover.png' | |
if shutil.which('ffmpeg') is None: | |
print('Unable to find ffmpeg.') | |
sys.exit(1) | |
def process_file(audio_file: str) -> str: | |
abs_image_file = os.path.abspath(INPUT_AUDIO_DIR + '/' + audio_file) | |
abs_audio_file = os.path.abspath(image_file) | |
video_file = os.path.splitext(audio_file)[0] + '.mp4' | |
abs_output_file = os.path.abspath(OUPUT_VIDEO_DIR + '/' + video_file) | |
# Create directory if necessary. | |
os.makedirs(os.path.dirname(abs_output_file), exist_ok=True) | |
# Process with ffmpeg. | |
ffmpeg_args = [ | |
'ffmpeg', | |
'-y', # Overwrite existing file. | |
'-r', '1', # Framerate of 1 fps. | |
'-loop', '1', # Loop on the image for video. | |
'-i', abs_audio_file, # Input audio. | |
'-i', abs_image_file, # Input image. | |
'-c:a', 'libmp3lame', # Audio codec | |
'-b:a', '320k', # Audio bitrate. | |
'-s', '1080x1080', # Size of the video output. | |
'-c:v', 'libx264', # Video codec. | |
'-preset', 'fast', # Make smallest video possible. #veryslow | |
'-crf', '0', # Constate rate factor (0 means lossless). | |
'-shortest', # Use audio duration. | |
abs_output_file | |
] | |
returncode = subprocess.call(ffmpeg_args) | |
# Exit if any error. | |
if returncode != 0: | |
sys.exit(returncode) | |
return abs_output_file | |
for audio_file in audio_files: | |
print(f'Processing: \'{audio_file}\'…') | |
output_file = process_file(audio_file) | |
print(f'Created {output_file}') | |
print('Done.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment