Skip to content

Instantly share code, notes, and snippets.

@Krzysiu
Last active August 22, 2016 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Krzysiu/7fce1250bff11ae7529285ea574ab440 to your computer and use it in GitHub Desktop.
Save Krzysiu/7fce1250bff11ae7529285ea574ab440 to your computer and use it in GitHub Desktop.
Youtube-dl wrapper which asks for format before downloading, uses simultaneous connections and more.
#!/usr/bin/env bash
# /----------------------------------------------------------------------------\
# | getVideo b2 |
# | (C) krzysiu.net 2016, multilicensed as MIT, LGPL and CC BY 4.0 |
# | Requires: |
# | * youtube-dl |
# | * aria2c |
# | |
# | Installation: |
# | 1) Save script body to getVideo |
# | 2) Go to the directory of the script and enter: |
# | chmod +x getVideo |
# | 3) If you don't have requiments, use following commands (nneds sudo): |
# | (for youtube-dl) ./getVideo --installreq ytdl |
# | (for aria2c, needs apt-get) ./getVideo --installreq |
# | |
# | Usage: |
# | ./getVideo [url] |
# | url - the URL supported by youtube-dl (video link, playlist etc.) |
# | |
# | Features: |
# | * getVideo.sh first asks for video format and then downloads it |
# | * can use simultaneous connections for faster downloading |
# | * automatic youtube-dl update if download fails (you need sudo for that) |
# | * you can set default output directory |
# | |
# | Aaaand... |
# | * Donations via PayPal are welcome: http://goo.gl/WGwuLM |
# | * To get different audio and video formats, enter format as "video+audio",|
# | e.g. if 19 is the code of audio and 50 is video format, enter 50+19 |
# | * I'm not "basher", "bashist" nor "bashovsky" or "bashnick". It could be |
# | written better, but it does it's job. |
# | * It can update youtube-dl only if it wasn't installed via apt-get, which |
# | shouldn't be used, as apt-get package is rarely updated |
# | * For some things youtube-dl might need ffmpeg |
# | |
# \----------------------------------------------------------------------------/
# ----USER CONFIG STARTS HERE----
# Max simultaneous connections; 3 is a good value
maxChunks=3
# Max chunk size, i.e. if minChunkSize is 50M and maxChunks is 3 then 3 chunks
# Will be used for movies bigger than maxChunks*minChunkSize=150M. 110 MB movie
# will use 2 chunks, 70 MB will use 1
minChunkSize="50M"
# Output directory WITH trailing slash. Leave empty for none
outputDir="/home/krzysiu/filmy/"
# Title prefix and suffix. See youtube-dl manual (-o parameter) for more info.
# E.g. with titlePrefix "[%(extractor)s %(height)sp] " sets the output filename
# to something like "[720p YouTube] Some movie title.mp4"
titlePrefix="[%(extractor)s %(height)sp] "
titleSuffix=
# File allocation method. Possible Values: none, prealloc, trunc, falloc.
# Recommended value is "falloc", but it won't work on ext3 (ext4 is fine).
fileAllocMethod="falloc"
# ----USER CONFIG ENDS HERE----
if [[ $1 == --installreq ]]; then
if [[ $2 == ytdl ]]; then
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl
else
sudo apt-get install aria2c ffmpeg
fi
exit
fi
wasUpdated=false
youtube-dl -F $1
if (($? != 0)); then
read -p "Can't get format list; Try to update Youtube-dl? " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
sudo youtube-dl -U
if (($? != 0)); then
echo "Couldn't update Youtube-dl. Aborting"
exit 2
fi
wasUpdated=true
youtube-dl -F $1
if (($? != 0)); then
echo "Update didn't help. Aborting."
exit 1
fi
fi
vidFormat=
while [[ $vidFormat == "" ]]; do
read -p "Which format would you want to get? " vidFormat
if [[ $vidFormat == "" ]]; then
echo "You must provide answer; press CTRL+Z to exit"
echo $vidFormat
fi
done
youtube-dl -f $vidFormat -o "'$outputDir$titlePrefix%(title)s$titleSuffix.%(ext)s'" --external-downloader aria2c --external-downloader-args "'--min-split-size=$minChunkSize -x $maxChunks -s $maxChunks' --file-allocation=$fileAllocMethod" $1
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment