Last active
September 12, 2020 22:32
-
-
Save sarkrui/8c3cd7ae93246a7a4b4deb8122018b34 to your computer and use it in GitHub Desktop.
Download YouTube videos with optional arguments for trimming
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 | |
dirpath="/path/to/directory" | |
cd "$dirpath" | |
url=$1 | |
startTimestamps=$2 | |
endTimestamps=$3 | |
scriptname="segytb" | |
youtube-dl --external-downloader aria2c --external-downloader-args "--file-allocation=none -j16 -x16 -s16 -k10M" --merge-output-format mp4 --format bestvideo+bestaudio -o 'temp.mp4' $url | |
title=$(youtube-dl --get-filename -o '%(title)s' $url | sed "s/ /_/g") | |
title="${title}.mp4" | |
# If user input has more than on argument (other than URL), then execuate the FFmpeg trimming | |
if [ $# -eq 2 ] || [ $# -eq 3 ] || [ $# -eq 4 ] | |
then | |
ffmpeg -i temp.mp4 -ss $startTimestamps -to $endTimestamps -c copy $title | |
if [[ -f $dirpath/$title ]]; then | |
echo "FFmpeg trimming success!" | |
rm temp.mp4 | |
fi | |
else | |
mv temp.mp4 $title | |
echo "${title} Downloaded!" | |
fi |
You could of course remove argument of using external downloader: aria2c for multi-thread downloading.
aria2c --external-downloader-args "--file-allocation=none -j16 -x16 -s16 -k10M"
N.B. The script fetches the highest quality of video (even if it's .webm
format in 4K) and saves it as a mp4 file.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
aria2c
andffmpeg
dirpath
to a desired one./segytb.sh https://www.youtube.com/watch?v=a3ICNMQW7Ok
./segytb.sh https://www.youtube.com/watch?v=a3ICNMQW7Ok 00:00:05 00:00:10