Skip to content

Instantly share code, notes, and snippets.

@lguenth
Created March 9, 2024 16:00
Show Gist options
  • Save lguenth/484abfaefea975a70722c252f3b60d74 to your computer and use it in GitHub Desktop.
Save lguenth/484abfaefea975a70722c252f3b60d74 to your computer and use it in GitHub Desktop.
Script for ffmpeg: Splits media files with the given extension into 5 minute chunks
#!/bin/bash
# Check if the number of arguments is less than 1
if [ "$#" -lt 2 ]; then
echo "Usage: $0 <input_folder> <extension>"
exit 1
fi
# Path to the folder containing the files
input_folder="$1"
# File extension to target
extension="$2"
# Check if the input folder exists
if [ ! -d "$input_folder" ]; then
echo "Error: Input folder '$input_folder' does not exist."
exit 1
fi
# Loop through all files in the folder
for file in "$input_folder"/*."${extension}"; do
filename=$(basename -- "$file")
filename_without_extension="${filename%.*}"
# Check duration of the input file
duration=$(ffprobe -i "$file" -show_entries format=duration -v quiet -of csv="p=0")
# Check if the duration is longer than 5 minutes (300 seconds)
if (( $(echo "$duration > 300" | bc -l) )); then
# Split the file into 5 minute chunks
ffmpeg -i "$file" -c copy -map 0 -segment_time 300 -f segment -reset_timestamps 1 "${input_folder}/${filename_without_extension}-%d.${extension}"
else
echo "Skipping $filename, duration is less than 5 minutes."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment