media-artist_framework/_data/convert.sh

72 lines
1.9 KiB
Bash
Raw Permalink Normal View History

2025-04-29 00:49:00 +03:00
#!/bin/bash
# Usage: ./convert.sh -c <chapter_number> -n <name> -i <index> [-d]
# Example: ./convert.sh -c 3 -n building_systems -i 3 -d
#
# This will create a base filename: c03-building_systems-03
# Input file: orig/c03-building_systems-03.mov
# Output file: video/c03-building_systems-03.mp4
# If -d is provided, it will prompt to delete the original file after conversion.
usage() {
echo "Usage: $0 -c <chapter_number> -n <name> -i <index> [-d]"
exit 1
}
# Initialize variables
chapter=""
name=""
index=""
delete_flag=false
# Parse command-line options
while getopts "c:n:i:d" opt; do
case "$opt" in
c) chapter="$OPTARG" ;;
n) name="$OPTARG" ;;
i) index="$OPTARG" ;;
d) delete_flag=true ;;
*) usage ;;
esac
done
# Make sure all required options are provided
if [ -z "$chapter" ] || [ -z "$name" ] || [ -z "$index" ]; then
usage
fi
# Format chapter and index as two-digit numbers
formatted_chapter=$(printf "%02d" "$chapter")
formatted_index=$(printf "%02d" "$index")
# Construct the base filename: cXX-name_XX
BASENAME="c${formatted_chapter}-${name}-${formatted_index}"
# Construct the input and output filenames
INPUT="orig/${BASENAME}.mov"
OUTPUT="video/${BASENAME}.mp4"
echo "Converting..."
echo "Input file: $INPUT"
echo "Output file: $OUTPUT"
# Run ffmpeg with your chosen parameters
ffmpeg -i "$INPUT" -vf "scale=-2:720" -preset slow -crf 30 -b:v 500k -c:v libx264 -c:a aac -b:a 96k "$OUTPUT"
# Check if -d flag was used and prompt for deletion
if [ "$delete_flag" = true ]; then
while true; do
read -p "Do you want to delete the original file $INPUT? Y/n " confirm
if [[ -z "$confirm" || "$confirm" =~ ^[Yy]$ ]]; then
rm "$INPUT"
echo "Original file deleted."
break
elif [[ "$confirm" =~ ^[Nn]$ ]]; then
echo "Original file kept."
break
else
echo "Invalid input. Please enter Y/n."
fi
done
fi