Skip to content

Instantly share code, notes, and snippets.

@fofr
Created April 27, 2023 18:19
Show Gist options
  • Save fofr/14272e2e16694c1ebc07bf9163dc3946 to your computer and use it in GitHub Desktop.
Save fofr/14272e2e16694c1ebc07bf9163dc3946 to your computer and use it in GitHub Desktop.
Create video from text and audio
#!/bin/bash
# Check if the argument is provided
if [ -z "$1" ]; then
echo "Usage: $0 <filename_without_extension>"
exit 1
fi
# Define input files and output file
filename="$1"
wav_file="${filename}.wav"
txt_file="${filename}.txt"
output_file="${filename}.mp4"
# Get the length of the audio file in seconds
audio_length=$(ffprobe -i "${wav_file}" -show_entries format=duration -v quiet -of csv="p=0")
# Create an image with the text centered
convert -size 1280x720 -background black -fill white -gravity center -pointsize 24 -font "Arial" caption:@"${txt_file}" "${filename}_temp.png"
# Create a video from the image with the same length as the audio
ffmpeg -loop 1 -i "${filename}_temp.png" -c:v libx264 -t "${audio_length}" -pix_fmt yuv420p -vf "scale=1280:720" "${filename}_temp.mp4"
# Combine the video and audio
ffmpeg -i "${filename}_temp.mp4" -i "${wav_file}" -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 "${output_file}"
# Remove temporary files
rm "${filename}_temp.png"
rm "${filename}_temp.mp4"
echo "Video created: ${output_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment