Posted By

ksaver on 09/05/10


Tagged

Bash YouTube zenity


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

ksaver


EzTube: Playing YouTube Videos without Flash


 / Published in: Bash
 

URL: http://identi.ca/ksaver

Yes, my code sucks, but I hope it will be useful... :-)

  1. #! /usr/bin/env bash
  2. #
  3. # eztube2.sh
  4. # Playing Youtube Videos without flash, using mplayer.
  5. # Revised and $(hopefully) better version.
  6. # ksaver (at identi.ca), Sep 2010.
  7. # Public Domain Code.
  8. # Dependences: (Unix-like OS, python, bash implicit).
  9. # Youtube-dl: http://bitbucket.org/rg3/youtube-dl/
  10. # Zenity: http://freshmeat.net/projects/zenity
  11. # MPlayer: http://www.mplayerhq.hu/
  12. #
  13. # More Info: http://ur1.ca/0g3gu
  14.  
  15. scriptname=$(basename $0)
  16. version='0.2.1'
  17. dependences='zenity mplayer youtube-dl'
  18.  
  19. #Edit this as needed/
  20. workdir="$HOME/Video"
  21. wait_sec=60
  22. ##/
  23.  
  24. function check_dep()
  25. {
  26. for DEPEND in "$@"
  27. do
  28. /usr/bin/which $DEPEND &> /dev/null ||\
  29. (echo "[ERR]: Dependence '$DEPEND' Not found in your path!" &&\
  30. return 1)
  31.  
  32. done
  33. }
  34.  
  35. function dl_video()
  36. {
  37. /usr/bin/env youtube-dl -q --continue "$1" -o "$2" &
  38. DLPID="$!"
  39. }
  40.  
  41. function get_url()
  42. {
  43. YTURL=$(zdialog --entry --text='Youtube Video URL:')
  44. }
  45.  
  46. function kill_dl()
  47. {
  48. PID=$1
  49. SEC=$2
  50. /bin/ps -p $PID &> /dev/null && waitload $SEC
  51. /bin/kill -15 $PID &> /dev/null
  52. }
  53.  
  54. function play_video()
  55. {
  56. /usr/bin/env mplayer -zoom "$1"
  57. }
  58.  
  59. function saveordrop()
  60. {
  61. zdialog --question --text="Save downloaded video?"
  62. }
  63.  
  64. function waitload()
  65. {
  66. #Not actually checking progress video size, just waits.
  67. CNT=0
  68. SEC=$1
  69. while [[ $CNT -lt $SEC ]]
  70. do
  71. echo $(($CNT*100/$SEC)) #%percent%
  72. let CNT=$CNT+1
  73. sleep 1
  74.  
  75. done | zdialog --progress --auto-close \
  76. --text="Loading video...\nPlease wait about $wait_sec secs."
  77. }
  78.  
  79. function zdialog()
  80. {
  81. /usr/bin/env zenity --title="$scriptname $version" "$@"
  82. }
  83.  
  84. function _main_()
  85. {
  86. check_dep $dependences || exit
  87.  
  88. YTURL=''
  89. while [[ ! $YTURL ]]
  90. do
  91. get_url || exit
  92. done
  93.  
  94. VIDNAME="$workdir/$(echo $YTURL |cut -d '&' -f 1 |cut -d '=' -f 2).flv"
  95. dl_video "$YTURL" "$VIDNAME"
  96.  
  97. # If file already exists play it
  98. if [[ -f $VIDNAME ]]
  99. then
  100. play_video "$VIDNAME"
  101.  
  102. else
  103. waitload $wait_sec
  104. play_video "$VIDNAME"
  105.  
  106. fi
  107.  
  108. #Save or delete file
  109. saveordrop
  110. if [ $? -eq 0 ]
  111. then
  112. FILENAME=$(zdialog --file-selection --filename="$VIDNAME")
  113.  
  114. #wait to download...
  115. kill_dl $DLPID $wait_sec
  116.  
  117. #and move downloaded video to new destiny
  118. /bin/mv "$VIDNAME" "$FILENAME"
  119.  
  120. else
  121. #kill download and delete file
  122. kill_dl $DLPID 0
  123. if [ -f "$VIDNAME" ]
  124. then
  125. /bin/rm "$VIDNAME"
  126. fi
  127.  
  128. fi
  129. }
  130.  
  131. #Run script...
  132. while true
  133. do
  134. _main_
  135. done

Report this snippet  

You need to login to post a comment.