testsAndMisc/linux_configuration/scripts/misc/testsAndMisc-bash/convert.sh

87 lines
2.3 KiB
Bash
Raw Normal View History

#!/bin/bash
# Default values
TARGET_EXT="mp4"
TARGET_SIZE=10M
# Parse arguments
if [ -n "$1" ]; then
INPUT_PATH="$1"
else
INPUT_PATH="."
fi
if [ -n "$2" ]; then
TARGET_EXT="$2"
fi
if [ -n "$3" ]; then
TARGET_SIZE="$3"
fi
# Create output directory
OUTPUT_DIR="converted"
mkdir -p "$OUTPUT_DIR"
# Function to convert video
convert_video() {
local input_file="$1"
local output_file="$OUTPUT_DIR/${input_file%.*}.$TARGET_EXT"
2025-11-06 19:39:04 +01:00
# Get video duration in seconds
DURATION=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$input_file")
echo "Duration: $DURATION seconds"
2025-11-06 19:39:04 +01:00
# Convert target size to bytes
TARGET_SIZE_BYTES=$(numfmt --from=iec "$TARGET_SIZE")
2025-11-06 19:39:04 +01:00
# Calculate target bitrate in kilobits per second
TARGET_BITRATE=$(echo "($TARGET_SIZE_BYTES * 8) / $DURATION / 2000" | bc)
2025-11-06 19:39:04 +01:00
# Convert video
2024-12-05 19:36:29 +01:00
ffmpeg -i "$input_file" -vcodec libx264 -b:v "${TARGET_BITRATE}k" -preset veryslow -acodec aac -c:a copy "$output_file"
2025-11-06 19:39:04 +01:00
# Get original and converted video sizes
ORIGINAL_SIZE=$(stat -c%s "$input_file")
CONVERTED_SIZE=$(stat -c%s "$output_file")
2025-11-06 19:39:04 +01:00
# Print out details
2025-11-06 19:39:04 +01:00
echo "Original size: $(numfmt --to=iec "$ORIGINAL_SIZE")"
echo "Video length: $DURATION seconds"
echo "Target size: $TARGET_SIZE"
2025-11-06 19:39:04 +01:00
echo "Converted size: $(numfmt --to=iec "$CONVERTED_SIZE")"
echo "Target bitrate: ${TARGET_BITRATE}kbps"
}
# Function to move video if already below target size and in desired format
move_video() {
local input_file="$1"
local output_file="$OUTPUT_DIR/${input_file##*/}"
2025-11-06 19:39:04 +01:00
# Get original video size
ORIGINAL_SIZE=$(stat -c%s "$input_file")
2025-11-06 19:39:04 +01:00
# Check if video is below target size and in desired format
2025-11-06 19:39:04 +01:00
if [[ $ORIGINAL_SIZE -le $TARGET_SIZE_BYTES && ${input_file##*.} == "$TARGET_EXT" ]]; then
mv "$input_file" "$output_file"
echo "Moved $input_file to $output_file"
else
convert_video "$input_file"
fi
}
# Export functions for find command
export -f convert_video
export -f move_video
export TARGET_EXT
export TARGET_SIZE
export TARGET_SIZE_BYTES
export OUTPUT_DIR
# Find and process videos
if [ -d "$INPUT_PATH" ]; then
2024-12-16 18:09:26 +01:00
find "$INPUT_PATH" \( -name "*.mkv" -o -name "*.mp4" -o -name "*.avi" -o -name "*.webm" \) -type f -exec bash -c 'move_video "$0"' {} \;
else
move_video "$INPUT_PATH"
fi