Last active
April 25, 2023 06:46
-
-
Save amurgit/d147692d52a4e7b6d1c01dabe923f62c to your computer and use it in GitHub Desktop.
N shades of gray - video generator (using ffmpeg)
This file contains hidden or 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
#!/bin/sh | |
set -euo pipefail | |
# Check if FFmpeg is installed and display an error message if not | |
if ! [ -x "$(command -v ffmpeg)" ]; then | |
echo 'Error: FFmpeg is not installed.' >&2 | |
exit 1 | |
fi | |
############### User defined params ################### | |
# Set the duration of each video in seconds, float values allowed e.g. 0.1 | |
duration=0.5 | |
# Video resolution | |
resolution="1366x768" | |
# Set font color of text label | |
fontcolor="white" | |
# Set the font color of label to white | |
fontfile="/Library/Fonts/Arial Unicode.ttf" | |
# Set the x and y position of the text label | |
x="(w-text_w)/4" | |
y="(h-text_h)/4" | |
# How many steps and min,max color value | |
min=0; max=255; steps=50 | |
output_file_name="${steps}_shades_of_gray_${duration}_${resolution}.mp4" | |
echo "Resolution: ${resolution}" | |
echo "Each step duration: ${duration} seconds" | |
echo "Step count: ${steps}" | |
[ ! -f "$fontfile" ] && { echo "Font file ${fontfile} does not exist!" >&2; exit 1; } | |
# Remove videos generated before | |
rm -rf ./tmp; mkdir ./tmp | |
# Remove the files.txt file if it exists, will be used to join all videos to one | |
tmp_file_list="tmp/files.txt" | |
[ -f "$tmp_file_list" ] && rm "$tmp_file_list" | |
interval=$(echo "$max / ${steps}" | bc -l) | |
step_num=0 | |
echo "Going to generate $((steps+1)) videos with ffmpeg" | |
for i in $(seq $min $interval $max); do | |
color_int=$(echo $i|cut -d. -f1) #sh's printf requires integers | |
color=$(printf "0x%02x%02x%02x" $color_int $color_int $color_int) | |
# Make fontcolor of text lable black if step number > steps/2 | |
(( $(echo "$step_num > ($steps / 2)" |bc -l) )) && fontcolor="black" | |
# Save file_name | |
fname="v${step_num}_${color}_${duration}sec_${resolution}.mp4"; fpath="tmp/$fname" | |
label="${step_num}/${steps}" | |
echo "file '${fname}'" >> "$tmp_file_list" | |
# Use FFmpeg to create a video with a solid color background, | |
# with a text overlay that displays the number and its value (e.g. "3/10") | |
ffmpeg -loglevel error -f lavfi -i color=c=${color}:s=${resolution} -vf \ | |
"drawtext=fontfile=${fontfile}: text='${label}': \ | |
fontsize=24: fontcolor=${fontcolor}: \ | |
x=${x}: y=${y}" \ | |
-t ${duration} "${fpath}" | |
echo "Generating ${fpath}" | |
((step_num++)) | |
done | |
[ -f "${output_file_name}" ] && rm ${output_file_name} | |
# Use FFmpeg to concatenate all the videos and save the result to output.mp4 | |
echo "Joining videos into single file: ${output_file_name}" | |
ffmpeg -f concat -i "$tmp_file_list" -c copy ${output_file_name} | |
echo "Done. File saved as \"${output_file_name}\"" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment