Last active
January 3, 2018 09:55
-
-
Save ryu1kn/5354abe83eb7780565cb30f6dd61fd01 to your computer and use it in GitHub Desktop.
mp4 downloader
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
#!/bin/bash | |
# Download MP4 files. Run the command with --help option to see more details | |
COMMAND_NAME=./mp4-download.sh | |
LOGFILE=download.log | |
FILENAME="" | |
ARGV_BASE_INDEX=1 | |
ARGV_DEBUG=NO | |
ARGV__=() | |
main() { | |
parseArgs "$@" | |
if [ $ARGV_DEBUG = YES ] ; then | |
set -x | |
fi | |
BASE_INDEX=$ARGV_BASE_INDEX | |
M3U8_URLS=`grep -v '^\s*#' ${ARGV__[0]}` | |
for M3U8_URL in $M3U8_URLS ; do | |
setFileName $BASE_INDEX | |
if [ -f $FILENAME ] ; then echo File $FILENAME already exists; exit 1; fi | |
downloadFile $M3U8_URL $FILENAME | |
BASE_INDEX=$(($BASE_INDEX + 1)) | |
done | |
} | |
setFileName() { | |
local INDEX=$1 | |
local TWO_DIGITS_INDEX=`echo $INDEX | sed 's/^\([[:digit:]]\)$/0\1/'` | |
FILENAME=$TWO_DIGITS_INDEX.mp4 | |
} | |
downloadFile() { | |
local M3U8_URL=$1 | |
local FILENAME=$2 | |
echo Downloading to $FILENAME... | |
ffmpeg -i "$M3U8_URL" -movflags faststart -c copy -bsf:a aac_adtstoasc $FILENAME >> $LOGFILE 2>&1 | |
} | |
parseArgs() { | |
local OPTION_KEY | |
while [[ $# -gt 0 ]] ; do | |
OPTION_KEY="$1" | |
case $OPTION_KEY in | |
-h|--help) | |
printUsage | |
exit 0 | |
;; | |
-b|--base-index) | |
ARGV_BASE_INDEX="$2" | |
shift; shift | |
;; | |
-d|--debug) | |
ARGV_DEBUG=YES | |
shift | |
;; | |
*) | |
ARGV__+=("$1") | |
shift | |
;; | |
esac | |
done | |
set -- "${POSITIONAL[@]}" # restore positional parameters | |
} | |
printUsage() { | |
cat << EOF | |
Usage: $COMMAND_NAME [-b base_number] file | |
Options: | |
-h, --help output usage information | |
-b <base_number> Number that downloaded file starts with | |
file File that contains list of .m3u8 file URLs | |
EOF | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment