#!/bin/sh

# script for downloading youtube videos and split it into pieces..

# defining shell colors for distinction
c_red="\033[1;31m"
c_green="\033[1;32m"
c_yellow="\033[1;33m"
c_blue="\033[1;34m"
c_magenta="\033[1;35m"
c_cyan="\033[1;36m"
c_reset="\033[0m"

#function to download full video
full_video()
{
	youtube-dl -f best "$1"
}

# function to download part of a video and also split it for SNS...
part_video()
{
	video=$1
	printf "${c_magenta}Enter Starting Point(hh:mm:ss)or(mm:ss)${c_reset}:"
	read start
	printf "${c_yellow}Enter Upto Seconds:${c_cyan}"
	read dur
	printf "Do you want to split video for SNS?? (y or n) :"
	read x
	printf "\n\n"
	if [ $x == 'n' ]; then		
		ffmpeg -i $(youtube-dl -f best --get-url "$video") -ss $start -t $dur $HOME/Videos/$(date +%T_%F).mp4
	else
		out=$(date +%T_%F)
		ffmpeg -i $(youtube-dl -f best --get-url "$video") -map 0 -codec copy -f segment -segment_time 30 -ss $start -t $dur $HOME/Videos/${out}_%02d.mp4
	fi
}

#program starts from here..

printf "${c_blue}Input Youtube link :${c_magenta}"
read x
printf "${c_yellow}Download>>\n${c_green}[f]Full video\n${c_cyan}[p]Part of video\n${c_red}[q]Quit"
printf "\n${c_reset}Enter choice:${c_green}"
read choice
case $choice in
	f)
		full_video "$x";;
	p)
		part_video "$x";;
	q)
		break;;
esac
