Skip to content

Instantly share code, notes, and snippets.

@lyjia
Created January 20, 2025 02:02
Show Gist options
  • Save lyjia/5f8a50bfbe1671061c38076c16ed1eeb to your computer and use it in GitHub Desktop.
Save lyjia/5f8a50bfbe1671061c38076c16ed1eeb to your computer and use it in GitHub Desktop.
This script combines an audio file (like a DJ mix) and a static image (like a mix cover) and into a video file using ffmpeg, suitable for upload to YouTube.
# This script combines an audio file (like a DJ mix) and a static image (like a mix cover) and into a video file using ffmpeg, suitable for upload to YouTube."
# (C) 2025 Lyjia - released as Creative Commons Attribution-ShareAlike 4.0 International - https://creativecommons.org/licenses/by-sa/4.0/
#!/usr/bin/env python3
import subprocess
import argparse
def create_video(image_path,
audio_path,
output_path,
audio_codec,
audio_bitrate,
video_codec):
"""
Combines a static image with an audio file into a video file using ffmpeg.
Args:
image_path (str): Path to the static image file.
audio_path (str): Path to the audio file.
output_path (str): Path for the output video file.
"""
try:
# ffmpeg command to create a video
command = [
'ffmpeg',
'-loop', '1',
'-i', image_path,
'-i', audio_path,
'-c:v', video_codec,
'-c:a', audio_codec,
'-b:a', audio_bitrate+"k",
'-pix_fmt', 'yuv420p',
'-shortest',
output_path
]
# Run the ffmpeg command
subprocess.run(command, check=True)
print(f"Video successfully created: {output_path}")
except subprocess.CalledProcessError as e:
print(f"Error occurred while creating the video: {e}")
except FileNotFoundError:
print("ffmpeg is not installed or not found in your PATH.")
def main():
# Parse command-line arguments
parser = argparse.ArgumentParser(
description="This script combines an audio file (like a DJ mix) and a static image (like a mix cover) and into a video file using ffmpeg, suitable for upload to YouTube."
)
parser.add_argument(
'-i', '--image', type=str, help="Path to the input static image file"
)
parser.add_argument(
'-a', '--audio', type=str, help="Path to the input audio file"
)
parser.add_argument(
'-o', '--output', type=str, help="Path to the output video file"
)
parser.add_argument(
'--ac', type=str, help="Audio codec to use", default="aac"
)
parser.add_argument(
'--ab', type=str, help="Audio bitrate to use (in kilobits, e.g. '128')", default="128"
)
parser.add_argument(
'--vc', type=str, help="Video codec to use", default="libx264")
args = parser.parse_args()
# Call the create_video function with the parsed arguments
create_video(image_path=args.image, audio_path=args.audio, output_path=args.output, audio_bitrate=args.ab, audio_codec=args.ac, video_codec=args.vc)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment