diff --git a/aliases b/aliases index fc9109c..7bdfb33 100644 --- a/aliases +++ b/aliases @@ -510,6 +510,10 @@ dl_twitch_chat() { # using the full path to the cookies file, e.g. # `tw-1080p60 --cookies /c//twitch_cookies.txt` # +# To extract a portion of a video, you have to first download the entire file and then +# pass these args to ffmpeg: `-ss HH:MM:SS : start time to take`, `-to HH:MM:SS : end time`. +# Or use the script `compress-video-portion` +# dl_twitch_vid() { local format="$1" local shortname="$2" diff --git a/bin/compress-video b/bin/compress-video index dcf021e..d51db04 100644 --- a/bin/compress-video +++ b/bin/compress-video @@ -23,21 +23,20 @@ else NORMAL="" fi -if [[ $1 == "" || $2 == "" ]]; then - printf "${BOLD}${RED}Usage: $0 ${NORMAL}\n" +use_gpu=$1 +filename=$(basename -- "$2") +output_name="$3" + +if [[ $use_gpu == "" || $2 == "" || $output_name == "" ]]; then + printf "${BOLD}${RED}Usage: $0 ${NORMAL}\n" exit 1 fi -use_gpu=$1 - # Found the following to work best with vids containing text (e.g. programming vid): 25 for CPU encoding and 27 for GPU. use_crf=25 if [[ $use_gpu -eq 1 ]]; then use_crf=27 fi -filename=$(basename -- "$2") -output_name="$3" - compress-video-with-crf $use_gpu $use_crf "$filename" "$output_name" diff --git a/bin/compress-video-portion b/bin/compress-video-portion new file mode 100644 index 0000000..8105a65 --- /dev/null +++ b/bin/compress-video-portion @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +if which tput >/dev/null 2>&1; then + ncolors=$(tput colors) +fi +if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then + RED="$(tput setaf 1)" + GREEN="$(tput setaf 2)" + YELLOW="$(tput setaf 3)" + BLUE="$(tput setaf 4)" + MAGENTA="$(tput setaf 5)" + CYAN="$(tput setaf 6)" + BOLD="$(tput bold)" + NORMAL="$(tput sgr0)" +else + RED="" + GREEN="" + YELLOW="" + BLUE="" + MAGENTA="" + CYAN="" + BOLD="" + NORMAL="" +fi + +use_gpu=$1 +filename=$(basename -- "$2") +output_name="$3" +start_time="$4" +end_time="$5" + +if [[ $use_gpu == "" || $2 == "" || $output_name == "" || $start_time == "" || $end_time == "" ]]; then + printf "${BOLD}${RED}Usage: $0 ${NORMAL}\n" + exit 1 +fi + +# Found the following to work best with vids containing text (e.g. programming vid): 25 for CPU encoding and 27 for GPU. +use_crf=25 +if [[ $use_gpu -eq 1 ]]; then + use_crf=27 +fi + +compress-video-with-crf $use_gpu $use_crf "$filename" "$output_name" "$start_time" "$end_time" + diff --git a/bin/compress-video-with-crf b/bin/compress-video-with-crf index 603866e..3c16911 100644 --- a/bin/compress-video-with-crf +++ b/bin/compress-video-with-crf @@ -23,42 +23,45 @@ else NORMAL="" fi -if [[ $1 == "" || $2 == "" ]]; then - printf "${BOLD}${RED}Usage: $0 \n\nIf you want to encode a range of CRF values then use -1 as the crf value.${NORMAL}\n" +use_gpu=$1 +use_crf=$2 +output_name="$4" +start_time="$5" +end_time="$6" + +if [[ $use_gpu == "" || $use_crf == "" || $3 == "" || $output_name == "" || ($start_time != "" && $end_time == "") ]]; then + printf "${BOLD}${RED}Usage: $0 \n\nIf you want to encode a range of CRF values then use -1 as the crf value.${NORMAL}\n" exit 1 fi -use_gpu=$1 -use_crf=$2 filename=$(basename -- "$3") extension="${filename##*.}" filename="${filename%.*}" -output_name="$4" - function encode() { crf=$1 - if [[ $output_name == "" ]]; then - output="${filename}_COMPRESSED_CRF-${crf}.$extension" - else - output="${output_name}.$extension" - fi + output="${output_name}.$extension" - printf "\n${YELLOW}${BOLD}Encoding '$filename.$extension' | GPU: $use_gpu | CRF $crf | output: $output${NORMAL}\n" + printf "\n${YELLOW}${BOLD}Encoding '$filename.$extension' | GPU: $use_gpu | CRF $crf | output: $output | start: $start_time | end: $end_time${NORMAL}\n" + + timing_args="" + if [[ $start_time != "" ]]; then + timing_args+="-ss $start_time -to $end_time" + fi if [[ $use_gpu -eq 1 ]]; then # File will be slightly larger than CPU encoding, but it's much faster to transcode and doesn't max out the CPU cores. #ffmpeg version N-103152-gef54590f83-20210806 # This version of ffmpeg results in larger files than version 4.3.1 command below. - #ffmpeg -y -stats -loglevel level+error -vsync 0 -hwaccel_output_format cuda -c:v h264_cuvid -i "$filename.$extension" -c:a copy -c:v h264_nvenc -profile:v high -rc:v vbr -cq:v $crf -b:v 5M -maxrate 5M -max_muxing_queue_size 9999 "$output" + #ffmpeg -y -stats -loglevel level+error $timing_args -vsync 0 -hwaccel_output_format cuda -c:v h264_cuvid -i "$filename.$extension" -c:a copy -c:v h264_nvenc -profile:v high -rc:v vbr -cq:v $crf -b:v 5M -maxrate 5M -max_muxing_queue_size 9999 "$output" #ffmpeg version 4.3.1-2020-11-08 - ffmpeg -y -stats -loglevel level+error -vsync 0 -hwaccel cuvid -c:v h264_cuvid -i "$filename.$extension" -c:a copy -c:v h264_nvenc -profile:v high -rc:v vbr_hq -cq:v $crf -b:v 5M -maxrate 5M -max_muxing_queue_size 9999 "$output" + ffmpeg -y -stats -loglevel level+error $timing_args -vsync 0 -hwaccel cuvid -c:v h264_cuvid -i "$filename.$extension" -c:a copy -c:v h264_nvenc -profile:v high -rc:v vbr_hq -cq:v $crf -b:v 5M -maxrate 5M -max_muxing_queue_size 9999 "$output" else - ffmpeg -i "$filename.$extension" -c:v libx264 -crf $crf -preset veryfast -profile:v baseline -level 3.0 -strict -2 "$output" + ffmpeg $timing_args -i "$filename.$extension" -c:v libx264 -crf $crf -preset veryfast -profile:v baseline -level 3.0 -strict -2 "$output" fi printf "\n${GREEN}${BOLD}Done encoding '$filename.$extension' (CRF $crf) | output name '$output'${NORMAL}\n\n" }