Skip to content

Instantly share code, notes, and snippets.

@jcrubino
Last active May 9, 2023 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcrubino/e4581cf0752c0cc94873c361ae353892 to your computer and use it in GitHub Desktop.
Save jcrubino/e4581cf0752c0cc94873c361ae353892 to your computer and use it in GitHub Desktop.
Bash File to Prepare a Folder of Videos
#!/bin/bash
# Sanitize all filenames in the current directory using detox
for file in *
do
new_file=$(detox "$file")
if [ -n "$new_file" ] && [ "$new_file" != "$file" ] && [ ! -e "$new_file" ]
then
mv "$file" "$new_file"
fi
done
# Create folders for each video file and move the file into the folder
for file in *.mp4 *.mov *.webm *.mkv
do
if [ -f "$file" ]
then
# Create a folder for the video file using the sanitized filename
folder=$(basename "$file" | sed 's/\.[^\.]*$//' | sed 's/[^[:alnum:]\.]/_/g')
mkdir -p "$folder"
mv "$file" "$folder/"
# Get video resolution
dimensions=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 "$folder/$file")
horizontal_dimensions=$(echo "$dimensions" | cut -d ',' -f 1)
vertical_dimensions=$(echo "$dimensions" | cut -d ',' -f 2)
framerate=$(ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 "$folder/$file")
rounded_framerate=$(echo "($framerate+0.5)/1+1" | bc)
# Extract audio from the video file and move to audio folder in the video folder
ffmpeg -i "$folder/$file" -vn -acodec pcm_s16le -ar 44100 "$folder/audio.wav"
mkdir -p "$folder/audio"
mv "$folder/audio.wav" "$folder/audio/"
# Get audio bitrate and write dimensions and bitrate to the filename
bitrate=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "$folder/audio/audio.wav")
bitrate=$(expr "$bitrate" / 1000)
filename="${horizontal_dimensions}_${vertical_dimensions}_${rounded_framerate}fps_${bitrate}kbs.txt"
echo "Video Resolution: $horizontal_dimensions x $vertical_dimensions" > "$folder/$filename"
echo "Audio Bitrate: $bitrate kbps" >> "$folder/$filename"
echo "Video Framerate: $framerate fps" >> "$folder/$filename"
# Print Seperator to stdout
for i in {1..3}
do
printf '%*s\n' 90 | tr ' ' '*'
done
# Print Seperator to stdout
echo "$folder/$filename"
for i in {1..3}
do
printf '%*s\n' 90 | tr ' ' '*'
done
# Extract images from the video file at the original framerate and move to images folder in the video folder
mkdir -p "$folder/images"
ffmpeg -i "$folder/$file" -q:v 0 -r "$framerate" -vf "scale='if(gt(iw,ih),min(256,iw),-1)':'if(gt(iw,ih),-1,min(256,ih))':force_original_aspect_ratio=decrease" "$folder/images/images-%03d.jpg"
fi
done
# Remove empty directories
rmdir * >/dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment