david/scripts-archive
david
/
scripts-archive
Archived
1
0
Fork 0
This repository has been archived on 2022-04-16. You can view files and clone it, but cannot push or open issues or pull requests.
scripts-archive/youtube_converter.sh

141 lines
4.1 KiB
Bash

#!/bin/bash
# # # # # # # # # # # # # # # # # # # # #
# #
# YouTube Contvertion Tool #
# #
# Author: david@socialnerds.org #
# Version: v0.3 #
# #
# #
# # # # # # # # # # # # # # # # # # # # #
# help text
if [[ $1 == "-h" || $1 == "--help" ]]; then
echo "
# # # # # # # # # # # # # # # # # # # # #
# #
# YouTube Contvertion Tool #
# #
# Author: david@socialnerds.org #
# Version: v0.3 #
# #
# #
# # # # # # # # # # # # # # # # # # # # #"
echo
echo "usage: youtube_converter <mp3/mpeg> <youtube-link> <destination path>"
echo "hint: all options are optional"
echo
exit 0
fi
# checking dependencies (except browser, i use chromium here)
if [[ $(aptitude search ffmpeg | grep multimedia | awk '{print $1}') = "i" && $(aptitude search lame | grep frontend | awk '{print $1}') = "i" && $(aptitude search curl | grep "Get a file" | awk '{print $1}') = "i" ]]; then
#echo "info: all dependencies found"
:
else
if [ $(whoami) = "root" ]; then
echo "info: trying to insall missing dependencies"
apt-get install -y ffmpeg curl lame
echo "info: all dependencies installed. rerun script."
exit 0
else
echo "error: permission denied"
echo "info: install dependencies(ffmpeg, curl and lame) or run this scrip as superuser"
exit 1
fi
fi
# check if entered link or given link option a youtube link
while [[ ${link:0:31} != "http://www.youtube.com/watch?v=" && ${2:0:31} != "http://www.youtube.com/watch?v=" ]]; do
echo "enter youtube link:"
read link
done
if [ -z $link ]; then
link=$2
fi
# do you want a mp3 or a mpeg
if [[ $1 = "mp3" || $1 = "mpeg" ]]; then
mediatype=$1
else
while [[ $mediatype != "mp3" && $mediatype != "mpeg" ]]; do
echo 'Do you want an audio or video file? mp3/mpeg[default is mp3].'
read mediatype
if [ -z $mediatype ]; then
mediatype="mp3"
fi
done
fi
# set output destination
if [ -z $3 ];then
destinationpath=$(pwd)
else
destinationpath=$3
fi
# getting flash filename
file=$(ls -l /tmp | grep Flash | awk '{print $8}')
secondfile=$(echo $file | awk '{print $2}')
if [ -z "$file" ]; then
echo "info: there is no open youtube window, trying to start browser"
#chromium-browser $link
firefox $link &
sleep 6
file=$(ls -l /tmp | grep Flash | awk '{print $8}')
secondfile=$(echo $file | awk '{print $2}')
fi
# checking if there is a second flash file in /tmp
if [ -z $secondfile ]; then
i="doitagain"
oldsize=$(ls -l /tmp | grep $file | awk '{print $5}')
sleep 5
while [ $i = "doitagain" ]; do
size=$(ls -l /tmp | grep $file | awk '{print $5}')
if [ $size -gt $oldsize ];then
echo "info: download not finished yet. waiting 5 seconds.."
oldsize=$size
sleep 5
else
echo "info: download finished."
i="goforit"
fi
done
else
echo 'error: there should just be one open video window (if no multiple youtube videos open try "rm -rf /tmp/Flash*")'
exit 1
fi
# parse link for title
curl "$link" --output tmp.txt
rawtitle=$(cat tmp.txt | w3m -dump -T text/html | sed -n '5p')
title=$rawtitle
#rawtitle=$(cat tmp.txt | grep 'meta name="title"')
rm tmp.txt
#count=${#rawtitle}
#length=$(($count-36))
#title=${rawtitle:34:$length}
# here starts the actual convertion
echo "starting convertion of $title @ $link"
if [ $mediatype != "mpeg" ]; then
ffmpeg -i /tmp/$file -vn temp.wav
lame --preset 128 temp.wav "$destinationpath/$title.mp3"
rm temp.wav
title="$title.mp3"
else
ffmpeg -i /tmp/$file -sameq -ab 192k "$destinationpath/$title.mpeg"
title="$title.mpeg"
fi
echo
echo "Output file $destinationpath/$title"
echo
exit 0