Skip to content

Instantly share code, notes, and snippets.

@sarkrui
Last active September 12, 2020 22:32
Show Gist options
  • Save sarkrui/8c3cd7ae93246a7a4b4deb8122018b34 to your computer and use it in GitHub Desktop.
Save sarkrui/8c3cd7ae93246a7a4b4deb8122018b34 to your computer and use it in GitHub Desktop.
Download YouTube videos with optional arguments for trimming
#!/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
@sarkrui
Copy link
Author

sarkrui commented Sep 11, 2020

  1. Install aria2c and ffmpeg
  2. Define the dirpath to a desired one
  3. Execute the below command up to your need
./segytb.sh https://www.youtube.com/watch?v=a3ICNMQW7Ok 
./segytb.sh https://www.youtube.com/watch?v=a3ICNMQW7Ok 00:00:05 00:00:10
  1. Done!

@sarkrui
Copy link
Author

sarkrui commented Sep 11, 2020

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