Last active
December 11, 2018 22:43
-
-
Save MarkKoz/d39573392c89a16a13cd10e34cf852ed to your computer and use it in GitHub Desktop.
Uses ffmpeg to convert ShadowPlay recordings so that they can be uploaded to Discord
This file contains hidden or 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
#!/bin/bash | |
# | |
# Uses ffmpeg to convert ShadowPlay recordings for uploading to Discord. | |
# Usage: ./8mib.sh <video_path> [seek_eof] | |
if [ ! -f "$1" ]; then | |
(>&2 echo "The video file could not be found.") | |
exit | |
fi | |
file_name="${1%.*}" | |
# 8 mebibytes expressed in bits. 512 bytes leeway for request. | |
MAX_SIZE=$(( (1024 * 1024 * 8 - 512) * 8 )) | |
# Sum bitrates of all audio streams. | |
audio_bitrate="$( | |
ffprobe \ | |
-v error \ | |
-select_streams a \ | |
-show_entries stream=bit_rate \ | |
-of default=noprint_wrappers=1:nokey=1 \ | |
"$1" \ | |
| awk '{sum+=$1} END {print sum}' | |
)" | |
if [ -z "$2" ]; then | |
seek="" | |
duration="$( | |
ffprobe \ | |
-v error \ | |
-show_entries format=duration \ | |
-of default=noprint_wrappers=1:nokey=1 \ | |
"$1" | |
)" | |
else | |
seek="-sseof -$2" | |
duration="$( | |
echo "$2" \ | |
| sed -r 's/(.*):(.+):(.+)/\1*3600+\2*60+\3/;s/(.+):(.+)/\1*60+\2/' \ | |
| bc -l | |
)" | |
fi | |
# Ceiling division, 2% overhead | |
video_bitrate="$( | |
echo "((${MAX_SIZE} + ${duration} - 1) / ${duration} - ${audio_bitrate}) * 0.98" \ | |
| bc | |
)" | |
bufsize="$(echo "${video_bitrate} / 2" | bc)" | |
# Two-pass H.264 @ 720p30 | |
ffmpeg \ | |
-hide_banner -loglevel 32 -y \ | |
$seek \ | |
-i "$1" \ | |
-c:v libx264 \ | |
-x264-params nal-hrd=cbr \ | |
-b:v "${video_bitrate}" \ | |
-minrate "${video_bitrate}" \ | |
-maxrate "${video_bitrate}" \ | |
-bufsize "${bufsize}" \ | |
-r 30 \ | |
-vf scale=-1:720 \ | |
-pass 1 \ | |
-passlogfile "${file_name}_2pass" \ | |
-an \ | |
-f mp4 /dev/null \ | |
&& ffmpeg \ | |
-hide_banner -loglevel 32 \ | |
$seek \ | |
-i "$1" \ | |
-c:v libx264 \ | |
-x264-params nal-hrd=cbr \ | |
-b:v "${video_bitrate}" \ | |
-minrate "${video_bitrate}" \ | |
-maxrate "${video_bitrate}" \ | |
-bufsize "${bufsize}" \ | |
-r 30 \ | |
-vf scale=-1:720 \ | |
-pass 2 \ | |
-passlogfile "${file_name}_2pass" \ | |
-c:a copy \ | |
-map 0:v -map 0:a \ | |
"${file_name}_8mb.mp4" | |
# Clean up pass log files. | |
find . -name "${file_name}_2pass*.log*" -type f -delete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment