Skip to content

Instantly share code, notes, and snippets.

@GRomR1
Last active December 4, 2022 17:15
Show Gist options
  • Save GRomR1/b32e3e175e094f456e7aae02ad3d7d7f to your computer and use it in GitHub Desktop.
Save GRomR1/b32e3e175e094f456e7aae02ad3d7d7f to your computer and use it in GitHub Desktop.
#!/bin/bash
###############################################################################
# Download full video from conference
###############################################################################
# Arguments:
# $1 - base url for download video from conference
# Use:
# ./download-video.sh \
# https://<URL>/vod/update/1669845928/61c4b2fd3a781d75_3_48524.mp4/media_0.ts
###############################################################################
set -euo pipefail
BASE_URL=$(echo ${1} | sed -r 's/media_[0-9]+\.ts/media_/g')
OUTPUT_DIR="./videos"
VIDEOS_LIST_FILE="./video_files.txt"
FULL_VIDEO_FILE="./full_video.mp4"
REMOVE_OUTPUT_DIR_AFTER_FINISH='true'
mkdir -p ${OUTPUT_DIR}
rm -rf ${VIDEOS_LIST_FILE}
# Usage: bannerColor "my title" "red" "*"
function bannerColor() {
case ${2} in
black) color=0
;;
red) color=1
;;
green) color=2
;;
yellow) color=3
;;
blue) color=4
;;
magenta) color=5
;;
cyan) color=6
;;
white) color=7
;;
*) echo "color is not set"; exit 1
;;
esac
local msg="${3} ${1} ${3}"
local edge
edge=${msg//?/$3}
tput setaf ${color}
tput bold
echo "${edge}"
echo "${msg}"
echo "${edge}"
tput sgr 0
echo
}
if [ ! "$(command -v ffmpeg)" ]; then
bannerColor "Command \"ffmpeg\" does not exist on system. Run 'brew install ffmpeg'" "red" "*"
exit -1
fi
for((i=0;i<1000;i++)); do
if [ -f "${OUTPUT_DIR}/video${i}.ts" ]; then
echo "File \"${OUTPUT_DIR}/video${i}.ts\" exists. Continue."
echo "file ${OUTPUT_DIR}/video${i}.ts" >> ${VIDEOS_LIST_FILE}
continue
fi
# echo "BASE_URL=${BASE_URL}${i}.ts"
status=$(curl -s -o /dev/null -w "%{http_code}" ${BASE_URL}${i}.ts)
# echo status=$status
if [[ "${status}" == "200" ]]; then
# echo "${BASE_URL}${i}.ts"
curl -s -o "${OUTPUT_DIR}/video${i}.ts" "${BASE_URL}${i}.ts"
echo "file ${OUTPUT_DIR}/video${i}.ts" >> ${VIDEOS_LIST_FILE}
echo "Success download file $(basename ${BASE_URL}${i}.ts)"
else
echo "BASE_URL=${BASE_URL}${i}.ts"
echo "Not correct status=$status! All videos download. Break it"
break
fi
done
echo "Concatenate video files to ${FULL_VIDEO_FILE}.tmp.ts"
ffmpeg -f concat -safe 0 -i ${VIDEOS_LIST_FILE} -c copy ${FULL_VIDEO_FILE}.tmp.ts
echo "Convert ${FULL_VIDEO_FILE}.tmp.ts to mp4"
ffmpeg -i ${FULL_VIDEO_FILE}.tmp.ts -bsf:a aac_adtstoasc -acodec copy -vcodec copy ${FULL_VIDEO_FILE}
# remove unused directories
if [[ "${REMOVE_OUTPUT_DIR_AFTER_FINISH}" == "true" ]]; then
echo "Remove downloaded video files"
rm -rf ${OUTPUT_DIR}
fi
rm -rf ${FULL_VIDEO_FILE}.tmp.ts
rm -rf ${VIDEOS_LIST_FILE}
bannerColor "Success! Open a file - ${FULL_VIDEO_FILE}" "green" "*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment