|
png2mp4() { |
|
local max_images="" |
|
local step_multiplier="" |
|
local repeat=1 |
|
local temp_dir="/home/kade/.local/tmp" |
|
|
|
while [[ "$#" -gt 0 ]]; do |
|
case $1 in |
|
--max) max_images="$2"; shift ;; |
|
--step) step_multiplier="$2"; shift ;; |
|
--repeat) repeat="$2"; shift ;; |
|
*) echo "Unknown parameter passed: $1"; return 1 ;; |
|
esac |
|
shift |
|
done |
|
|
|
echo "Creating temporary directory..." |
|
mkdir -p "$temp_dir" |
|
|
|
echo "Checking for PNG files..." |
|
png_files=($(/usr/bin/env ls *.png 2>/dev/null)) |
|
if [ ${#png_files[@]} -eq 0 ]; then |
|
echo "Error: No PNG files found in the current directory." |
|
return 1 |
|
fi |
|
|
|
|
|
local samples=($(ls *.png | sed -n 's/.*_\([0-9][0-9]\)_.*/\1/p' | sort -u)) |
|
|
|
for sample in "${samples[@]}"; do |
|
local output_filename="$(basename "$(pwd)")_sample${sample}.mp4" |
|
echo "Processing sample ${sample}. Output filename: $output_filename" |
|
|
|
echo "Creating repeated images for sample ${sample}..." |
|
for img in *_${sample}_*.png; do |
|
for i in $(seq 1 $repeat); do |
|
cp "$img" "$temp_dir/${img%.*}_${i}.png" |
|
done |
|
done |
|
|
|
echo "Running ffmpeg for sample ${sample}..." |
|
local vf_options="scale=1024x1024" |
|
if [[ -n "$step_multiplier" ]]; then |
|
vf_options+=",drawtext=fontfile=/usr/share/fonts/TTF/Inconsolata-Light.ttf:text='Steps\: %{expr\:trunc(n*$step_multiplier/$repeat/1000000)}':x=10:y=h-th-10:fontsize=24:fontcolor=white" |
|
fi |
|
|
|
if [[ -n "$max_images" ]]; then |
|
vf_options="select='not(mod(n\,$max_images))',$vf_options" |
|
fi |
|
|
|
ffmpeg -framerate 60 -pattern_type glob -i "$temp_dir/*_${sample}_*.png" -vf "$vf_options" -crf 18 \ |
|
-c:v libx264 -b:v 12M -pix_fmt yuv420p -y "$temp_dir/temp_${sample}.mp4" |
|
|
|
if [ $? -ne 0 ]; then |
|
echo "Error: ffmpeg command failed for sample ${sample}." |
|
rm -rf "$temp_dir" |
|
return 1 |
|
fi |
|
|
|
echo "Processing final video for sample ${sample}..." |
|
duration=$(ffmpeg -i "$temp_dir/temp_${sample}.mp4" 2>&1 | grep 'Duration' | awk '{print $2}' | tr -d , | awk -F: '{print ($1 * 3600) + ($2 * 60) + $3}') |
|
fade_start=$(echo "$duration + 3" | bc) |
|
ffmpeg -i "$temp_dir/temp_${sample}.mp4" -vf "tpad=stop_mode=clone:stop_duration=8,fade=t=out:st=$fade_start:d=5" -c:v libx264 -b:v 12M -crf 18 -pix_fmt yuv420p -y "$output_filename" |
|
|
|
if [ $? -ne 0 ]; then |
|
echo "Error: Final ffmpeg processing failed for sample ${sample}." |
|
rm -rf "$temp_dir" |
|
return 1 |
|
fi |
|
|
|
|
|
rm "$temp_dir"/*_${sample}_*.png "$temp_dir/temp_${sample}.mp4" |
|
done |
|
|
|
echo "Cleaning up temporary directory..." |
|
rm -rf "$temp_dir" |
|
|
|
echo "All samples processed successfully." |
|
} |
|
|