Skip to content

Instantly share code, notes, and snippets.

@damienix
Created December 5, 2010 23:24
Show Gist options
  • Save damienix/729592 to your computer and use it in GitHub Desktop.
Save damienix/729592 to your computer and use it in GitHub Desktop.
Downloads videos loaded by flashplugin.
#!/bin/sh
#: TItle : ytdownload
#; Date : 2010-12-12
#: Modified : 2010-12-14
#: Author : Damian Skrodzki <damienix1@gmail.com>
#: Version : 1.2
#: Description : Downloads videos loaded by flashplugin.
#: TODO : - command line interface
# Temp infos file
TMP="/tmp/ytdl"
search() {
# Grab needed infos
lsof -w | grep -E "[a-zA-Z]+ +[0-9]+ + [^0-9]+ .+/tmp/Flash.*" > $TMP \
| dialog --backtitle "YTDownload" --infobox "Searching..." 3 15
# Count number of videos
NUMBER=0
while read; do ((NUMBER++)); done < $TMP
}
getinfo() {
# Genering needed information
i=1;
while read CMD PID USER FD TYPE DEV SIZE NODE NAME DEL; do
FD=${FD%%[a-z]} # del character
NAME[$i]=${NAME##*/} # get filename
SOURCE[$i]="/proc/$PID/fd/$FD"
INFO=$(ffmpeg -i ${SOURCE[$i]} 2>&1) # get video info using ffmpeg
TIME=$(echo $INFO | grep -E -o '[0-9]+:[0-9]+:[0-9]+.[0-9]+')
TIME[$i]=${TIME:3:5} # clean hours and miliseconds from time
RES=$(echo $INFO | grep -E -o ' [0-9]+x[0-9]+ ')
RES[$i]=${RES// /} # clean enclosing spaces from resolution
((i++))
done < $TMP
}
# Main program
main() {
search
# Variables
EXT=flv # Extension
DEST=$PWD # Destination dir
# If there are no films
if [ $NUMBER -eq 0 ]; then
dialog --title "Searching finished." \
--msgbox "There are no vidios in cache." 5 35
exityt
# If there are some videos
else
getinfo
# Show menu
while [ ${ENDMAIN:=0} -eq 0 ]; do
sel=$(dialog --stdout --title "Choose option" \
--backtitle "YTDownload" \
--menu "Found $NUMBER videos." 13 35 8 \
1 "Download all" \
2 "Download chosen" "" "-------------------" \
3 "Options" \
4 "Search again" \
5 "Exit")
# If esc then exit
[ $? == 1 ] && ENDMAIN=1
case $sel in
# Download all
1) download $( seq 1 $NUMBER )
# End program if downloaded
ENDMAIN=1;;
# Dowlnoad selected
2)
# Make list with films to $VLIST
generatelist
results=$(dialog --stdout \
--title "Founded vidios"\
--checklist "Choose to download:" 12 40 6 $VLIST)
if [ $? == 0 ]
then
# Set counters to zero
l=0;
# Count videos to download
for result in $results; do ((l++)); done
# If some vidios checked
if [ $l -gt 0 ]; then
download ${results//\"/} # Delete unneeded quotes "
ENDMAIN=1
else
dialog --title "No videos to download!" \
--msgbox "Return to menu." 6 35
fi
fi;;
3) optionsbox;;
4) search
getinfo;;
5) ENDMAIN=1;;
esac
done
fi
}
generatelist() {
VLIST=
for (( i=1; i <= $NUMBER; i++ ))
do
# prints: 'i time_resolution ' to TMP_STR
printf -v TMP_STR "%s %s on " $i \
"${TIME[$i]}${RES[$i]:+"_${RES[$i]}"}"
# Add entry to list
VLIST+=$TMP_STR
done
}
optionsbox() {
SEL=$(dialog --stdout --title "Opcje" --backtitle "YTDownload"\
--menu "" 10 40 5 \
1 "Output format" \
2 "Output directory" \
3 "Name format" \
4 "Back")
if [ $? == 0 ]; then
case $SEL in
1) EXT=$(dialog --stdout --title "Choose format"\
--backtitle "YTDownload" \
--menu "Avaiable extensions:" 13 40 5 \
'flv' "" 'avi' "" 'mpg' '' 'mp4' '' 'mp3' '') ;;
2) ND=$(dialog --stdout\
--title "Choose directory:" --fselect "$DEST" 5 50)
if [ $? = 0 ]; then
# Check if new dir exists
if [ -d "$ND" ]; then
DEST="$ND"
dialog --title "Chosen new directory." \
--msgbox "$DEST" 7 40
else
dialog --title "Directory does not exist!" \
--msgbox "$ND" 7 40
fi
fi ;;
3) PATTERN=$(dialog --stdout --title "Give pattern for filenames" --inputbox "%n - video number\n%f - original flash video name\n%r - resolution\n%d - duration" 11 40)
esac
fi
}
download() {
# Downloads films of given numbers as $@
c=0
# If some vidios checked
for v in $@
do
((c++))
# Print progress for gauge
printf "XXX\nDownloading $c from $#.\nXXX\n$((100*$c/$#))"
_download $v
done | dialog --title "Downloading" \
--gauge "" 6 40 0
}
_download() {
# Downloads movie number $1
CURR=${PATTERN//%n/"$1"}
CURR=${CURR//%f/"${NAME[$1]}"}
CURR=${CURR//%r/"${RES[$1]}"}
CURR=${CURR//%d/"${TIME[$1]}"}
# Check if conversion is needed
if [ $EXT = 'flv' ]
then
# Just direct vidio to file
cat ${SOURCE[$1]} > ${CURR:-"${NAME[$1]}"}.$EXT
else
# Convert to chosen format and save
ffmpeg -i ${SOURCE[$1]} -y "${CURR:-"${NAME[$1]}"}.$EXT" 2>/dev/null
fi
}
exityt() {
[ -f $TMP ] && rm -r $TMP
clear
exit 0
}
################ STARTING ###################
while getopts "vh" OPT
do
case $OPT in
# Help
h) printf "%s\n" \
"${0##*/} [-d] [-h]" \
" -h wyswietla to" \
" -v informacje o wersji"
exit 0;;
# Version
v) printf "%s\n" \
" YTDownloader" ""\
"Program do zapisywania na dysk plików wideo ładowanych do pamięci przez wtyczkę flashplugin." \
"Skrypt powstał jako projekt na przedmiot 'Systemy operacyjne' na politechnice Gdańskiej." ""\
"Kontakt:" \
" email: damienix1@gmail.com" \
" JID: damienix@jabber.gda.pl" \
" web: damienix.jogger.pl" ""\
"Copyright Damian Skrodzki, 2010"
exit 0;;
# Unsupported command
*) exit 1;;
esac
done
dialog --backtitle "YTDownload" --yesno "Choose \"YES\" to start searching, \
otherwise will exit." 6 40
case $? in
0) main && exityt;;
*) exityt;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment