Skip to content

Instantly share code, notes, and snippets.

@Thaodan
Last active December 21, 2015 06:38
Show Gist options
  • Save Thaodan/6265008 to your computer and use it in GitHub Desktop.
Save Thaodan/6265008 to your computer and use it in GitHub Desktop.
youget youtube video converter
#!/bin/sh
# download youtube videos and convert their soundline in Mp3 or Ogg vorbis file
# Copyright (C) 2013 Björn Bidar
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
detectDE()
# detect which DE is running
# taken from xdg-email script
{
if [ x"$KDE_FULL_SESSION" = x"true" ]; then
echo kde;
elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then
echo gnome;
elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then
echo gnome;
elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then
echo xfce;
else
echo generic
fi
}
d_msg() # display msgs and get input
#########################################################################################################################
# NOTE: needs kdialog ( or zenity ) to display graphical messages and get input in gui #
#########################################################################################################################
# usage: #
# d_msg [modifer] topic msg #
# modifers: #
# ! msg is an error/faile message #
# i msg is an msg/input ( work's not properly in cgi and with xmessage : terminal) #
# f msg is an yes/no msg/test #
# l msg is an list of items ( nyi in cgi: terminal) #
# no modifer msg is an normal msg #
#########################################################################################################################
# #
# vars: #
# DMSG_GUI_APP=`detectDE` (default) # d_msg detects wich DE is installed and uses the equal dialog for displaing #
# DMSG_GUI_APP=generic # only set if dialog for DE not found #
# DMSG_GUI_APP=gnome|xfce # with this you can force d_msg to use zenity #
# DMSG_GUI_APP=kde # with this you can force d_msg to use kdialog #
# #
# DMSG_GUI # if not zero use graphical dialog, else cfg gui #
# DMSG_ICON # icon that d_msg uses when is runned in gui mode if not set icon xorg is used #
# #
# #
# DMSG_APP # say DMSG to use $DMSG_APP in cli possible vara are dialog and cgi_dialog #
# #
# #
# #
# #
#########################################################################################################################
{
if [ ! $# -lt 2 ] ; then
unset dmsg_return_status
if [ "${DMSG_GUI}" = true ] || [ ! $DMSG_GUI = 0 ] ; then
if [ -z "$DMSG_GUI_APP" ] ; then
DMSG_DE=$(detectDE)
fi
case $DMSG_DE in
kde) DMSG_GUI_APP=kdialog ;;
gnome) DMSG_GUI_APP=zenity ;;
generic) DMSG_GUI_APP=kdialog ;;
case "$DMSG_GUI_APP" in
kde)
which kdialog > /dev/null || \
{ which > /dev/null zenity && DMSG_GUI_APP=gnome || DMSG_GUI_APP=generic; }
;;
gnome|xfce) which zenity > /dev/null || \
( which kdialog > /dev/null && DMSG_GUI_APP=kde || \
DMSG_GUI_APP=generic );;
*) which kdialog > /dev/null && DMSG_GUI_APP=kde || { which zenity > /dev/null && DMSG_GUI_APP=zenity \
|| DMSG_GUI_APP=generic; }
;;
esac
fi
case $DMSG_GUI_APP in
kde)
case $1 in
!) kdialog --icon ${DMSG_ICON:=xorg} --caption "${DMSG_APPNAME:=$appname}" --title "$2" --error "$3"
dmsg_return_status=${DMSG_ERR_STAUS:=1}
;;
i) kdialog --icon ${DMSG_ICON:=xorg} --caption "${DMSG_APPNAME:=$appname}" --title "$2" --inputbox "$3"
dmsg_return_status=$?
;;
l) kdialog --icon ${DMSG_ICON:=xorg} --caption "${DMSG_APPNAME:=$appname}" --title "$2" --menu \
"$3" "$4" "$5" "$6" "$7" "$8" "$9"
shift ; dmsg_return_status=$? ;;
f) kdialog --icon ${DMSG_ICON:=xorg} --caption "${DMSG_APPNAME:=$appname}" --title "$2" --yesno "$3"
dmsg_return_status=$? ;;
*) kdialog --icon ${DMSG_ICON:=xorg} --caption "${DMSG_APPNAME:=$appname}" --title "$1" --msgbox "$2"
dmsg_return_status=$? ;;
esac
;;
xfce|gnome) #nyi impleted
case $1 in
!) zenity --window-icon=${DMSG_ICON:=xorg} --title="$2 - ${DMSG_APPNAME:=$appname}" --error --text="$3"
dmsg_return_status=${DMSG_ERR_STAUS:=1}
;;
i) zenity --window-icon=${DMSG_ICON:=xorg} --title="$2 - ${DMSG_APPNAME:=$appname}" --entry --text="$3"
dmsg_return_status=$?
;;
l) zenity --window-icon=${DMSG_ICON:=xorg} --title="$2 -${APPNAME:=$appname}" --column='' --text="$3"\
--list
dmsg_return_status=$?
;;
f) zenity --window-icon=${DMSG_ICON:=xorg} --title="$2 -${APPNAME:=$appname}" --question --text="$3"
dmsg_return_status=$?
;;
*) zenity --window-icon=${DMSG_ICON:=xorg} --title="$1 -${APPNAME:=$appname}" --info --text="$2"
dmsg_return_status=$? ;;
esac
;;
*)
case $1 in
!) xmessage -center -title "$2 - ${APPNAME:=$appname}" "err: "$3"" ;
dmsg_return_status=${DMSG_ERR_STAUS:=1}
;;
f) xmessage -center -title "$2 -${APPNAME:=$appname}" -buttons no:1,yes:0 "$3"
dmsg_return_status=$?
;;
i)
if [ -z $buttons ] ; then
DMSG_XBUTTONS='not:1,set:2'
fi
xmessage -center -title "$appname - "$2"" -print -buttons $buttons "$3"
dmsg_return_status=$?
;;
l) xmessage -center -title "$2 - ${APPNAME:=$appname}" -print -buttons "$3","$4","$5","$6","$7","$8","$9" ; dmsg_return_status=$? ;;
*) xmessage -center -title "$1 - ${APPNAME:=$appname}" "$2" ; dmsg_return_status=$? ;;
esac
;;
esac
else
case ${DMSG_APP:-native} in
dialog)
case "$1" in
!) dialog --title "$2 -${APPNAME:=$appname}" --infobox "error:$3" 0 0 ; dmsg_return_status=${DMSG_ERR_STAUS:=1};;
#!) cgi_dialog ! "$3" ; dmsg_return_status=${DMG_ERR_STAUS:=1} ;;
f) dialog --title "$2 - ${APPNAME:=$appname}" --yesno "$3" 0 0
dmsg_return_status=$?
;;
i) dialog --title "$2 - ${APPNAME:=$appname}" --inputbox "$3" 0 0
dmsg_return_status=$?
;;
*) dialog --title "$1 -${APPNAME:=$appname}" --infobox "$2" 0 0 ;;
#*) cgi_dialog "$2" ; dmsg_return_status=$? ;;
esac
;;
native)
case "$1" in
!) echo "$3" ; dmsg_return_status=${DMSG_ERR_STAUS:=1} ;;
f) echo ""$3" y|n"
read a
if [ ! $a = y ] ; then
dmsg_return_status=1;
fi
;;
i)
echo "$3"
read a
if [ -z "$a" ] ; then
dmsg_return_status=1;
fi
;;
*) echo "$2" ; dsmg_return_status=$? ;;
esac
;;
esac
fi
fi
return $dmsg_return_status
}
default_decoder=mp3
decoder=$default_decoderB
which ffmpeg oggenc lame youtube-dl > /dev/null || exit $?
case $LANG in
de*)
del_msg="$vidname löschen?"
del_msg_end="$vidname wurde gelöscht"
err_input_message="Keine oder falsche Eingabe gegeben, um Hilfetext zu sehen youget -h eingeben"
err_download="Beim herunterladen trat ein Fehler auf."
err_convert="Beim convertieren trat ein Fehler auf."
err_delete_vidname="Beim löschen von $vidname trat ein Fehler auf."
help_msg="`cat <<-HELP
$appname Decoder Youtube-Adresse
Schalter:
Decoder:.
-mp3 Ausgabedatei hat das Mp3-Format
-ogg Ausgabedatei hat das Ogg-Vorbis Format
* Ausgabedatei hat das $default_decoder Format
-h --help Zeige diese Hilfe an
HELP`"
help=Hilfe
;;
en*|C)
del_msg="Delete $vidname ?"
del_msg_end="$vidname deleted"
err_input_message="No or wrong options given, enter youget -h for help"
err_download="Fail while downloading."
err_convert="Fail while converting."
err_delete_vidname="Error while delete $vidname"
# err_msg_2=
help_msg="`cat <<-HELP
$appname decoder Youtube-url
options:
decoders:
-mp3 output file has Mp3 format
-ogg output file has Ogg Vorbis format
* output file has $default_decoder format
-h --help display this help message
HELP`"
;;
esac
display_help () {
d_msg ${help:=Help} "$help_msg"
}
download () { # download youtube video
time=$(date +%s )
vidname=$( youtube-dl -e $1 | sed 's/ /_/g') && \
youtube-dl -q -o $vidname.mp4 $1 || \
return 1
}
convert () { # convert youtube videos audio line in Mp3 or Ogg file
ffmpeg -loglevel quiet -i $vidname.mp4 -vn $time.wav > /dev/null 2>&1
case $decoder in # if decoder=mp3 do first if decoder=ogg do second
mp3) lame --silent --preset 128 $time.wav $vidname.mp3 ;;
ogg) oggenc -Q -b 128 $time.wav -o $vidname.ogg ;;
esac && \
rm $time.wav || \
return 2
}
delete_video () { # ask if downloaded video file should deleted
d_msg f dell_file "$del_msg"
if [ $? = 0 ] ; then
rm $vidname.mp4 && d_msg dell "$del_msg_end"
fi || \
return 3
}
if [ $TERM = dumb ] ; then
DMSG_GUI=1
fi
if [ $# -ge 1 ] ; then
while [ ! $# = 0 ]; do
case $1 in
-mp3) decoder=mp3 ; shift ;; # download ; convert ; delete_video ;; ## Wenn man, dann muss die erste Eingabe -mp3 sein oder $default_decoder mp3 sein
-ogg) decoder=ogg ; shift ;; # download ; convert ; delete_video ;; ## Wenn man die Datei als OGG ausgeben möchte, dann muss die erste Eingabe -ogg sein oder $default_decoder ogg sein
-g) DMSG_GUI=1; shift;;
-h|--help) display_help ; exit 0 ;;
--) shift ; break;;
-*|--*) d_msg ! input "$err_input_message"; exit 1 ;;
*) break;;
esac
done
fi
if [ "$DMSG_GUI" = 1 ] || [ ! $# = 0 ] ; then
target=$(d_msg i input "Pleae enter a Youtube video url") || exit $?
download "$1" && \
convert && \
delete_video
case $? in
1) d_msg ! Error "$err_download" ;;
2) d_msg ! Error "$err_convert" ;;
3) d_msg ! Error "$err_delete_vidname" ;;
esac
else
d_msg ! Input "$err_input_message"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment