Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

60%
+1 −0
Q&A ffmpeg script outputs video with unexpected resolution and frame rate despite scaling and fps filtering

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...

posted 3mo ago by ShadowsRanger‭  ·  edited 3mo ago by ShadowsRanger‭

Answer
#3: Post edited by user avatar ShadowsRanger‭ · 2024-09-29T18:18:18Z (3 months ago)
  • 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 by user avatar ShadowsRanger‭ · 2024-09-29T18:17:33Z (3 months ago)
  • 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 by user avatar ShadowsRanger‭ · 2024-09-29T18:17:17Z (3 months ago)
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
```