Last active
April 3, 2024 11:36
-
-
Save CAMOBAP/e19f2d7dd7437fe8ece3dc7002aea0ff to your computer and use it in GitHub Desktop.
Update to support ffmpeg 6.1.1
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/bin/env bash | |
# | |
# Input inteface | |
# | |
YOUTUBE_VID_ID="" | |
DURATION="" | |
START_TIME="0:00" | |
OUTPUT="output.mp4" | |
SILENT="" | |
AUDIO_ONLY="" | |
FFFILTER="crop=435:285:0:805" | |
usage="$(basename "$0") [-h] [-s n] -- program to download video fragment from YouTube | |
where: | |
-h show this help text | |
-v set YouTube Video Id | |
-d set YouTube Video duration | |
-s set YouTube Video start time (default: ${START_TIME}) | |
-o output file (default: ${OUTPUT}) | |
-a audio only" | |
while getopts ':hyav:s:d:o:f:' option; do | |
case "$option" in | |
h) echo "$usage" | |
exit | |
;; | |
v) YOUTUBE_VID_ID=$OPTARG | |
;; | |
s) START_TIME=$OPTARG | |
;; | |
d) DURATION=$OPTARG | |
;; | |
o) OUTPUT=$OPTARG | |
;; | |
f) FFFILTER=$OPTARG | |
;; | |
y) SILENT="-y" | |
;; | |
a) AUDIO_ONLY="-y" | |
OUTPUT="output.aac" | |
;; | |
:) printf "! Missing argument for -%s. Check usage:\n\n" "$OPTARG" >&2 | |
echo "$usage" >&2 | |
exit 1 | |
;; | |
\?) printf "! Illegal option: -%s. Check usage:\n\n" "$OPTARG" >&2 | |
echo "$usage" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
# | |
# Input validation | |
# | |
[ -z "$YOUTUBE_VID_ID" ] && { printf "! Missing YouTube Video Id.\nAdd -v viDe01dENtiF.\n Exit"; exit 1; } | |
[ -z "$OUTPUT" ] && { printf "! Missing output file.\nAdd -o output.mp4.\n Exit"; exit 1; } | |
# | |
# Processing | |
# | |
URLS=( $(youtube-dl --youtube-skip-dash-manifest -g "https://www.youtube.com/watch?v=${YOUTUBE_VID_ID}") ) | |
VIDEOURL="${URLS[0]}" | |
AUDIOURL="${URLS[1]}" | |
YOUTUBE_TIME=$(echo "${START_TIME}" | tr ':' '\n' | tac | tr -s '\n' ':' | awk -F: '{ print ($3 * 3600) + ($2 * 60) + $1 }') | |
INPUTS_FFAGS="-ss ${START_TIME} -i "${AUDIOURL}"" | |
if [ ! -n "$AUDIO_ONLY" ]; then | |
INPUTS_FFAGS="${INPUTS_FFAGS} -ss ${START_TIME} -i "${VIDEOURL}"" | |
fi | |
DURATION_FFLAGS="" | |
if [ ! -z "$DURATION" ]; then | |
DURATION_FFLAGS="-t ${DURATION}" | |
fi | |
OUTPUT="-map 1:v -c:v libx264 ${OUTPUT}" | |
if [ ! -n "$AUDIO_ONLY" ]; then | |
OUTPUT="-map 0:a -c:a aac ${OUTPUT}" | |
fi | |
echo "ffmpeg ${SILENT} ${INPUTS_FFAGS} ${DURATION_FFLAGS} ${OUTPUT}" | |
ffmpeg ${SILENT} ${INPUTS_FFAGS} ${DURATION_FFLAGS} ${OUTPUT} | |
if [ ! -n "$AUDIO_ONLY" -a "${FFFILTER}" != "" ]; then | |
filename=$(basename -- "$OUTPUT") | |
CROPPED_OUTPUT="${filename%.*}-cropped.${filename##*.}" | |
ffmpeg ${SILENT} -i ${OUTPUT} \ | |
-filter:v "${FFFILTER}" \ | |
-metadata comment="https://www.youtube.com/watch?v=${YOUTUBE_VID_ID}&t=${YOUTUBE_TIME%.*} duration:${DURATION}" ${CROPPED_OUTPUT} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment