Created
September 23, 2019 06:01
-
-
Save gallo-s-chingon/bddcab264c008180a61ff2fa12cdcb80 to your computer and use it in GitHub Desktop.
screenshot and mediainfo video files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/bash | |
# | |
# Written by ump721 | |
# Based on and inspired by dsl420's original script | |
# | |
# Usage: | |
# mediashot video.mkv [destination_folder] [cap_name] [number_of_caps] | |
# | |
# Defaults: | |
# destination_folder same folder as input file | |
# cap_name input file name (plus appropriate extensions) | |
# number_of_caps 10 | |
# | |
# Example: | |
# mediashot my_video.mkv ~/capsToUpload my_video_cap 5 | |
# | |
# Would create the following files: | |
# ~/capsToUpload/my_video_cap.1.png | |
# ~/capsToUpload/my_video_cap.2.png | |
# ~/capsToUpload/my_video_cap.3.png | |
# ~/capsToUpload/my_video_cap.4.png | |
# ~/capsToUpload/my_video_cap.5.png | |
# ~/capsToUpload/my_video_cap.mediainfo | |
check_dependencies() { | |
check_dependency ffmpeg | |
check_dependency ffprobe | |
check_dependency mediainfo | |
} | |
check_dependency() { | |
local name=$1 | |
hash "$name" > /dev/null 2>&1 | |
if [ $? -eq 1 ]; then | |
echo "Missing dependency: ${name}" | |
exit 0 | |
fi | |
} | |
echo_red() { | |
local red="\033[0;31m" | |
local reset="\033[0m" # Clear set colors | |
local output=$* | |
echo -e "${red}${output}${reset}" | |
} | |
calc() { | |
local precision=3 | |
bc <<< "scale=${precision}; $*" | |
} | |
calc_frame_position() { | |
local duration=$1 | |
local total_frames=$2 | |
local current_frame=$3 | |
local offset | |
local increment | |
increment=$(calc "$duration / $total_frames") | |
offset=$(calc "$increment / 2") | |
calc "($increment * $current_frame) - $offset" | |
} | |
get_duration() { | |
local file=$1 | |
ffprobe -i "$file" -show_format -v quiet | sed -n 's/duration=//p' | |
} | |
write_screencap() { | |
local file=$1 | |
local position=$2 | |
local output_file=$3 | |
ffmpeg -ss "$position" -i "$file" -y -vframes 1 -vf "scale='max(sar,1)*iw':'max(1/sar,1)*ih'" -v quiet "${output_file}.png" | |
} | |
write_mediainfo() { | |
local file=$1 | |
local output=$2 | |
mediainfo "$file" > "${output}.mediainfo" | |
} | |
main() { | |
local input_file=$1 | |
local output_dir=$2 | |
local output_file_name=$3 | |
local number_of_caps=$4 | |
local output | |
local duration | |
mkdir "$output_dir" &> /dev/null | |
# Set defaults | |
output_dir=${output_dir:=$(dirname "$input_file")} | |
output_file_name=${output_file_name:=$input_file} | |
number_of_caps=${number_of_caps:=10} | |
if [ ! -f "$input_file" ]; then | |
echo_red "Input file not found: $input_file" | |
echo_red "Usage: mediashot INPUT [create_destination_folder] [cap_name] [number_of_caps]" | |
exit 1 | |
fi | |
# if [ ! -d "$output_dir" ]; then | |
# echo_red "Output directory not found: $output_dir" | |
# exit 1 | |
# fi | |
output="${output_dir}/${output_file_name}" | |
duration=$(get_duration "$input_file") | |
printf "Writing mediainfo... " | |
write_mediainfo "$input_file" "$output" | |
printf "done\n" | |
# create screen caps | |
for current_cap_index in $(seq -w 1 1 "$number_of_caps"); do | |
printf "Processing %s/%s... " "$current_cap_index" "$number_of_caps" | |
write_screencap "$input_file" \ | |
"$(calc_frame_position "$duration" "$number_of_caps" "$current_cap_index")" \ | |
"${output}.${current_cap_index}" | |
printf "done\n" | |
done | |
} | |
check_dependencies | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment