Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
This script applies filters dynamically based on the video's dimensions and frame rate. The audio handling is more sophisticated, checking and adjusting the bitrate separately. It's more flexible i...
#3: Post edited
- This script applies filters dynamically based on the video's dimensions and frame rate. The audio handling is more sophisticated, checking and adjusting the bitrate separately. It's more flexible in codec selection, only converting when truly necessary. It uses glob patterns for file matching, potentially offering better efficiency.
- ```bash
- #!/usr/bin/env bash
- # Constants
- MAX_RESOLUTION=540
MAX_FPS=24- MAX_BITRATE="500k"
- CRF_VALUE=30
MAX_AUDIO_BITRATE="128k"- # Check if the directory argument is provided
- if [ $# -eq 0 ]; then
- echo "Usage: $0 <directory>"
- exit 1
- fi
- TARGET_DIR="$1"
- # Function to log messages
- log() {
- echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
- }
- # Function to handle errors
- handle_error() {
- log "Error occurred: $1"
- exit 1
- }
- # Main conversion loop
- for FILE in "$TARGET_DIR"/*.{mp4,mkv,avi,mov,m4v,wmv}; do
- [ -e "$FILE" ] || continue
- log "Processing: $FILE"
- # Get video dimensions and frame rate
- DIMENSIONS=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$FILE")
- WIDTH=$(echo $DIMENSIONS | cut -d'x' -f1)
- HEIGHT=$(echo $DIMENSIONS | cut -d'x' -f2)
- FPS=$(ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=r_frame_rate -of csv=p=0 "$FILE" | bc -l | awk '{printf("%d\n",$1 + 0.5)}')
- FILTERS=()
- if [ $WIDTH -lt $HEIGHT ]; then
- SHORTER_SIDE=$WIDTH
- if [ $WIDTH -gt $MAX_RESOLUTION ]; then
- FILTERS+=("scale=$MAX_RESOLUTION:-2")
- fi
- else
- SHORTER_SIDE=$HEIGHT
- if [ $HEIGHT -gt $MAX_RESOLUTION ]; then
- FILTERS+=("scale=-2:$MAX_RESOLUTION")
- fi
- fi
- if [ $FPS -gt $MAX_FPS ]; then
- FILTERS+=("fps=$MAX_FPS")
- fi
- # Combine filters if any exist
- if [ ${#FILTERS[@]} -gt 0 ]; then
- FILTER_OPTION="-vf $(IFS=,; echo "${FILTERS[*]}")"
- else
- FILTER_OPTION=""
- fi
- audio_bitrate=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "$FILE")
- audio_filter=""
- if [ "$audio_bitrate" -gt "$MAX_AUDIO_BITRATE" ]; then
- audio_filter="-b:a $MAX_AUDIO_BITRATE"
- fi
- # Get codec info
- CODEC_INFO=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$FILE")
- # Check if conversion is needed
- if [[ "$CODEC_INFO" != "hevc" && "$CODEC_INFO" != "h265" ]] || [ "$FPS" -gt "$MAX_FPS" ] || [ $SHORTER_SIDE -gt $MAX_RESOLUTION ] || [ "$audio_bitrate" -gt "$MAX_AUDIO_BITRATE" ]; then
- OUTPUT_FILE="${FILE%.*}.h265.mkv"
- # Combine scaling, FPS filtering, and codec selection
- ffmpeg_cmd=(
- "-i" "$FILE"
- $FILTER_OPTION
- "-c:v" "libx265"
- "-preset" "slow"
- "-crf" "$CRF_VALUE"
- "-c:a" "aac"
- $audio_filter
- "-movflags" "+faststart"
- "$OUTPUT_FILE"
- )
- log "Executing FFmpeg command: ${ffmpeg_cmd[*]}"
- ffmpeg "${ffmpeg_cmd[@]}" || handle_error "FFmpeg failed to encode $FILE"
- log "Conversion completed successfully."
- else
- log "No conversion needed for $FILE"
- fi
- done
- ```
- This script applies filters dynamically based on the video's dimensions and frame rate. The audio handling is more sophisticated, checking and adjusting the bitrate separately. It's more flexible in codec selection, only converting when truly necessary. It uses glob patterns for file matching, potentially offering better efficiency.
- ```bash
- #!/usr/bin/env bash
- # Constants
- MAX_RESOLUTION=540
- MAX_FPS=15
- MAX_BITRATE="500k"
- CRF_VALUE=30
- MAX_AUDIO_BITRATE="64k"
- # Check if the directory argument is provided
- if [ $# -eq 0 ]; then
- echo "Usage: $0 <directory>"
- exit 1
- fi
- TARGET_DIR="$1"
- # Function to log messages
- log() {
- echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
- }
- # Function to handle errors
- handle_error() {
- log "Error occurred: $1"
- exit 1
- }
- # Main conversion loop
- for FILE in "$TARGET_DIR"/*.{mp4,mkv,avi,mov,m4v,wmv}; do
- [ -e "$FILE" ] || continue
- log "Processing: $FILE"
- # Get video dimensions and frame rate
- DIMENSIONS=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$FILE")
- WIDTH=$(echo $DIMENSIONS | cut -d'x' -f1)
- HEIGHT=$(echo $DIMENSIONS | cut -d'x' -f2)
- FPS=$(ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=r_frame_rate -of csv=p=0 "$FILE" | bc -l | awk '{printf("%d\n",$1 + 0.5)}')
- FILTERS=()
- if [ $WIDTH -lt $HEIGHT ]; then
- SHORTER_SIDE=$WIDTH
- if [ $WIDTH -gt $MAX_RESOLUTION ]; then
- FILTERS+=("scale=$MAX_RESOLUTION:-2")
- fi
- else
- SHORTER_SIDE=$HEIGHT
- if [ $HEIGHT -gt $MAX_RESOLUTION ]; then
- FILTERS+=("scale=-2:$MAX_RESOLUTION")
- fi
- fi
- if [ $FPS -gt $MAX_FPS ]; then
- FILTERS+=("fps=$MAX_FPS")
- fi
- # Combine filters if any exist
- if [ ${#FILTERS[@]} -gt 0 ]; then
- FILTER_OPTION="-vf $(IFS=,; echo "${FILTERS[*]}")"
- else
- FILTER_OPTION=""
- fi
- audio_bitrate=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "$FILE")
- audio_filter=""
- if [ "$audio_bitrate" -gt "$MAX_AUDIO_BITRATE" ]; then
- audio_filter="-b:a $MAX_AUDIO_BITRATE"
- fi
- # Get codec info
- CODEC_INFO=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$FILE")
- # Check if conversion is needed
- if [[ "$CODEC_INFO" != "hevc" && "$CODEC_INFO" != "h265" ]] || [ "$FPS" -gt "$MAX_FPS" ] || [ $SHORTER_SIDE -gt $MAX_RESOLUTION ] || [ "$audio_bitrate" -gt "$MAX_AUDIO_BITRATE" ]; then
- OUTPUT_FILE="${FILE%.*}.h265.mkv"
- # Combine scaling, FPS filtering, and codec selection
- ffmpeg_cmd=(
- "-i" "$FILE"
- $FILTER_OPTION
- "-c:v" "libx265"
- "-preset" "slow"
- "-crf" "$CRF_VALUE"
- "-c:a" "aac"
- $audio_filter
- "-movflags" "+faststart"
- "$OUTPUT_FILE"
- )
- log "Executing FFmpeg command: ${ffmpeg_cmd[*]}"
- ffmpeg "${ffmpeg_cmd[@]}" || handle_error "FFmpeg failed to encode $FILE"
- log "Conversion completed successfully."
- else
- log "No conversion needed for $FILE"
- fi
- done
- ```
#2: Post edited
The second script applies filters dynamically based on the video's dimensions and frame rate. The audio handling is more sophisticated, checking and adjusting the bitrate separately. It's more flexible in codec selection, only converting when truly necessary. It uses glob patterns for file matching, potentially offering better efficiency.- ```bash
- #!/usr/bin/env bash
- # Constants
- MAX_RESOLUTION=540
- MAX_FPS=24
- MAX_BITRATE="500k"
- CRF_VALUE=30
- MAX_AUDIO_BITRATE="128k"
- # Check if the directory argument is provided
- if [ $# -eq 0 ]; then
- echo "Usage: $0 <directory>"
- exit 1
- fi
- TARGET_DIR="$1"
- # Function to log messages
- log() {
- echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
- }
- # Function to handle errors
- handle_error() {
- log "Error occurred: $1"
- exit 1
- }
- # Main conversion loop
- for FILE in "$TARGET_DIR"/*.{mp4,mkv,avi,mov,m4v,wmv}; do
- [ -e "$FILE" ] || continue
- log "Processing: $FILE"
- # Get video dimensions and frame rate
- DIMENSIONS=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$FILE")
- WIDTH=$(echo $DIMENSIONS | cut -d'x' -f1)
- HEIGHT=$(echo $DIMENSIONS | cut -d'x' -f2)
- FPS=$(ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=r_frame_rate -of csv=p=0 "$FILE" | bc -l | awk '{printf("%d\n",$1 + 0.5)}')
- FILTERS=()
- if [ $WIDTH -lt $HEIGHT ]; then
- SHORTER_SIDE=$WIDTH
- if [ $WIDTH -gt $MAX_RESOLUTION ]; then
- FILTERS+=("scale=$MAX_RESOLUTION:-2")
- fi
- else
- SHORTER_SIDE=$HEIGHT
- if [ $HEIGHT -gt $MAX_RESOLUTION ]; then
- FILTERS+=("scale=-2:$MAX_RESOLUTION")
- fi
- fi
- if [ $FPS -gt $MAX_FPS ]; then
- FILTERS+=("fps=$MAX_FPS")
- fi
- # Combine filters if any exist
- if [ ${#FILTERS[@]} -gt 0 ]; then
- FILTER_OPTION="-vf $(IFS=,; echo "${FILTERS[*]}")"
- else
- FILTER_OPTION=""
- fi
- audio_bitrate=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "$FILE")
- audio_filter=""
- if [ "$audio_bitrate" -gt "$MAX_AUDIO_BITRATE" ]; then
- audio_filter="-b:a $MAX_AUDIO_BITRATE"
- fi
- # Get codec info
- CODEC_INFO=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$FILE")
- # Check if conversion is needed
- if [[ "$CODEC_INFO" != "hevc" && "$CODEC_INFO" != "h265" ]] || [ "$FPS" -gt "$MAX_FPS" ] || [ $SHORTER_SIDE -gt $MAX_RESOLUTION ] || [ "$audio_bitrate" -gt "$MAX_AUDIO_BITRATE" ]; then
- OUTPUT_FILE="${FILE%.*}.h265.mkv"
- # Combine scaling, FPS filtering, and codec selection
- ffmpeg_cmd=(
- "-i" "$FILE"
- $FILTER_OPTION
- "-c:v" "libx265"
- "-preset" "slow"
- "-crf" "$CRF_VALUE"
- "-c:a" "aac"
- $audio_filter
- "-movflags" "+faststart"
- "$OUTPUT_FILE"
- )
- log "Executing FFmpeg command: ${ffmpeg_cmd[*]}"
- ffmpeg "${ffmpeg_cmd[@]}" || handle_error "FFmpeg failed to encode $FILE"
- log "Conversion completed successfully."
- else
- log "No conversion needed for $FILE"
- fi
- done
- ```
- This script applies filters dynamically based on the video's dimensions and frame rate. The audio handling is more sophisticated, checking and adjusting the bitrate separately. It's more flexible in codec selection, only converting when truly necessary. It uses glob patterns for file matching, potentially offering better efficiency.
- ```bash
- #!/usr/bin/env bash
- # Constants
- MAX_RESOLUTION=540
- MAX_FPS=24
- MAX_BITRATE="500k"
- CRF_VALUE=30
- MAX_AUDIO_BITRATE="128k"
- # Check if the directory argument is provided
- if [ $# -eq 0 ]; then
- echo "Usage: $0 <directory>"
- exit 1
- fi
- TARGET_DIR="$1"
- # Function to log messages
- log() {
- echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
- }
- # Function to handle errors
- handle_error() {
- log "Error occurred: $1"
- exit 1
- }
- # Main conversion loop
- for FILE in "$TARGET_DIR"/*.{mp4,mkv,avi,mov,m4v,wmv}; do
- [ -e "$FILE" ] || continue
- log "Processing: $FILE"
- # Get video dimensions and frame rate
- DIMENSIONS=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$FILE")
- WIDTH=$(echo $DIMENSIONS | cut -d'x' -f1)
- HEIGHT=$(echo $DIMENSIONS | cut -d'x' -f2)
- FPS=$(ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=r_frame_rate -of csv=p=0 "$FILE" | bc -l | awk '{printf("%d\n",$1 + 0.5)}')
- FILTERS=()
- if [ $WIDTH -lt $HEIGHT ]; then
- SHORTER_SIDE=$WIDTH
- if [ $WIDTH -gt $MAX_RESOLUTION ]; then
- FILTERS+=("scale=$MAX_RESOLUTION:-2")
- fi
- else
- SHORTER_SIDE=$HEIGHT
- if [ $HEIGHT -gt $MAX_RESOLUTION ]; then
- FILTERS+=("scale=-2:$MAX_RESOLUTION")
- fi
- fi
- if [ $FPS -gt $MAX_FPS ]; then
- FILTERS+=("fps=$MAX_FPS")
- fi
- # Combine filters if any exist
- if [ ${#FILTERS[@]} -gt 0 ]; then
- FILTER_OPTION="-vf $(IFS=,; echo "${FILTERS[*]}")"
- else
- FILTER_OPTION=""
- fi
- audio_bitrate=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "$FILE")
- audio_filter=""
- if [ "$audio_bitrate" -gt "$MAX_AUDIO_BITRATE" ]; then
- audio_filter="-b:a $MAX_AUDIO_BITRATE"
- fi
- # Get codec info
- CODEC_INFO=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$FILE")
- # Check if conversion is needed
- if [[ "$CODEC_INFO" != "hevc" && "$CODEC_INFO" != "h265" ]] || [ "$FPS" -gt "$MAX_FPS" ] || [ $SHORTER_SIDE -gt $MAX_RESOLUTION ] || [ "$audio_bitrate" -gt "$MAX_AUDIO_BITRATE" ]; then
- OUTPUT_FILE="${FILE%.*}.h265.mkv"
- # Combine scaling, FPS filtering, and codec selection
- ffmpeg_cmd=(
- "-i" "$FILE"
- $FILTER_OPTION
- "-c:v" "libx265"
- "-preset" "slow"
- "-crf" "$CRF_VALUE"
- "-c:a" "aac"
- $audio_filter
- "-movflags" "+faststart"
- "$OUTPUT_FILE"
- )
- log "Executing FFmpeg command: ${ffmpeg_cmd[*]}"
- ffmpeg "${ffmpeg_cmd[@]}" || handle_error "FFmpeg failed to encode $FILE"
- log "Conversion completed successfully."
- else
- log "No conversion needed for $FILE"
- fi
- done
- ```
#1: Initial revision
The second script applies filters dynamically based on the video's dimensions and frame rate. The audio handling is more sophisticated, checking and adjusting the bitrate separately. It's more flexible in codec selection, only converting when truly necessary. It uses glob patterns for file matching, potentially offering better efficiency.
```bash
#!/usr/bin/env bash
# Constants
MAX_RESOLUTION=540
MAX_FPS=24
MAX_BITRATE="500k"
CRF_VALUE=30
MAX_AUDIO_BITRATE="128k"
# Check if the directory argument is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <directory>"
exit 1
fi
TARGET_DIR="$1"
# Function to log messages
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Function to handle errors
handle_error() {
log "Error occurred: $1"
exit 1
}
# Main conversion loop
for FILE in "$TARGET_DIR"/*.{mp4,mkv,avi,mov,m4v,wmv}; do
[ -e "$FILE" ] || continue
log "Processing: $FILE"
# Get video dimensions and frame rate
DIMENSIONS=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$FILE")
WIDTH=$(echo $DIMENSIONS | cut -d'x' -f1)
HEIGHT=$(echo $DIMENSIONS | cut -d'x' -f2)
FPS=$(ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=r_frame_rate -of csv=p=0 "$FILE" | bc -l | awk '{printf("%d\n",$1 + 0.5)}')
FILTERS=()
if [ $WIDTH -lt $HEIGHT ]; then
SHORTER_SIDE=$WIDTH
if [ $WIDTH -gt $MAX_RESOLUTION ]; then
FILTERS+=("scale=$MAX_RESOLUTION:-2")
fi
else
SHORTER_SIDE=$HEIGHT
if [ $HEIGHT -gt $MAX_RESOLUTION ]; then
FILTERS+=("scale=-2:$MAX_RESOLUTION")
fi
fi
if [ $FPS -gt $MAX_FPS ]; then
FILTERS+=("fps=$MAX_FPS")
fi
# Combine filters if any exist
if [ ${#FILTERS[@]} -gt 0 ]; then
FILTER_OPTION="-vf $(IFS=,; echo "${FILTERS[*]}")"
else
FILTER_OPTION=""
fi
audio_bitrate=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "$FILE")
audio_filter=""
if [ "$audio_bitrate" -gt "$MAX_AUDIO_BITRATE" ]; then
audio_filter="-b:a $MAX_AUDIO_BITRATE"
fi
# Get codec info
CODEC_INFO=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$FILE")
# Check if conversion is needed
if [[ "$CODEC_INFO" != "hevc" && "$CODEC_INFO" != "h265" ]] || [ "$FPS" -gt "$MAX_FPS" ] || [ $SHORTER_SIDE -gt $MAX_RESOLUTION ] || [ "$audio_bitrate" -gt "$MAX_AUDIO_BITRATE" ]; then
OUTPUT_FILE="${FILE%.*}.h265.mkv"
# Combine scaling, FPS filtering, and codec selection
ffmpeg_cmd=(
"-i" "$FILE"
$FILTER_OPTION
"-c:v" "libx265"
"-preset" "slow"
"-crf" "$CRF_VALUE"
"-c:a" "aac"
$audio_filter
"-movflags" "+faststart"
"$OUTPUT_FILE"
)
log "Executing FFmpeg command: ${ffmpeg_cmd[*]}"
ffmpeg "${ffmpeg_cmd[@]}" || handle_error "FFmpeg failed to encode $FILE"
log "Conversion completed successfully."
else
log "No conversion needed for $FILE"
fi
done
```
