6

I was writing this small script the other day.It is a small program which basically finds the flash videos running in the browser and downloads them. the code goes something like this..

code:

path=$(stat -c %N /proc/*/fd/* 2>&1|awk -F[\`\'] '/lash/{print$2}')
pid=$(echo $path | cut -d / -f3)
name=$( ps --no-header -o comm -p $pid)
mid=$(wmctrl -l | grep -i $name)
filename=`echo $mid | cut -c 21-$(($(echo $mid | wc -c)-16))`
cp $path /home/Downloads/"$filename.avi"

this is just a PART of the code.The first line of the code gets the path to the deleted temp file.The second line gets the pid. Third the process name.Fourth the window id and the window title.The fifth line manipulates the wmctrl output to extract the name of the video playing(most of the time the name of the video is included in the window title..for example youtube)

Now when this script is run after the whole video has buffered,it runs flawlessly(this above script works for google chrome)..and copies the file from the /proc//fd folder to the downloads folder and changes its name to the extracted filename.

Now here comes the trick part.I want to automate this code,like,for me to run this code as of now i have to wait for the video to finish buffering(say youtube).

I want this script to determine when the video has finished buffering and then go ahead with the copying part without me having to manually run it at the end of a video.

So is there any way in which i can get to know when a video has completed buffering?

I am a beginner.Any suggestions for improving the code are welcome

4
  • Interesting idea. Do you have to worry about the tmp file disappearing after it is done playing or is that part of the browser cache that only gets deleted occasionally? Also, for the filename= section why do you use backticks, the whole beauty of $(...) for cmd-substitution is that you can nest them without any quoting issues. Good luck.
    – shellter
    Commented Dec 22, 2011 at 22:25
  • @shellter: Thanks mate! I figured it out finally.The file disappears only when the process is killed.That is, the file remains in the directory until the browser is closed.This is evident from the fact that videos(mainly from youtube) aren't downloaded when we replay them,they are infact played from the cache(or similar). And the backticks,well,i was getting an unexpected error. The logic is that the filesize(of the video being downloaded) keeps changing until the buffering is complete.So we check the filesize until it becomes constant and then trigger the download function. Commented Dec 23, 2011 at 0:21
  • Sorry was short on characters there.the code is monitor_size() { one=du -L "$path" | awk '{print $1}' sleep 5 two=du -L "$path" | awk '{print $1}' if [ $one -eq $two ] then download else monitor_size fi } wherein the download function contains the earlier code.Btw,i guess you were right about the disappearing of the tmp file.Streaming is different.Youtube merely downloads the files to your computer. :sigh: Commented Dec 23, 2011 at 0:29
  • OK, It sounds like you're all set? If not, please edit your original posting above to best problem statement. If you need my help, send me another comment here. Good luck
    – shellter
    Commented Dec 23, 2011 at 1:33

1 Answer 1

0

to get the file size, you can use:

stat -c %s $filename

with this you can set up a while loop to check if the file is still changing

CURSIZE=0
LASTSIZE=1
while ([ $CURSIZE -ne $LASTSIZE ]) do
  LASTSIZE=`stat -c %s $filename`
  sleep 1
CURSIZE=`stat -c %s $filename`
done

after that, you should be able to copy it assuming your internet connection doesn't hiccup for greater than the sleep value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.