A Script to Save Cached Flash Videos on Linux
By on July 21st, 2012

It happens so many times, just so many times — we watch a YouTube video in the browser and then want to have a copy of the same. But saving it directly from the browser requires addons/extensions, which may not be available instantly; actually, Chrome doesn’t need a restart after extension installation and you may be able to save the cached video instantly. Or may be not — I don’t know because I don’t use Chrome, but Firefox and I know this script works very well as I use it quite often :-)
Also, it is quite possible that an addon/extension is not able to get the video from cache, and starts downloading again (makes no sense actually).

The flashplayer is very smart, it deletes the file as soon as you’re done with watching the video, but thanks to Linux, we can still recover the video from memory. It’s a property of Linux that files don’t get deleted immediately. When a file is deleted, but is open by a program, the file is not removed from memory/disk (but is not visible in the file and directory listings) and can be easily recovered from the /proc virtual filesystem. I’ve written a small script to help this problem and it is available under GNU GPL license at https://github.com/nileshgr/utilities/blob/master/general/flashdownload.sh which uses this feature to copy the videos from memory into another location so that you can have a copy. A copy is pasted below as well.

#!/bin/bash

# Script by Nilesh Govindrajan <me@nileshgr.com>

# Saves cached flash video from any running browsers (that use libflashplayer.so)
# File will be saved as the random string with extension .flv in the current directory.

for p in $(pgrep -f libflashplayer.so -U `id -u`)
do
    for f in $(find /proc/$p/fd -type l)
    do
	filename=$(readlink $f)
	echo $filename | grep /tmp/Flash
	if [ $? -eq 0 ]
	then
	    dstfname=$(echo $filename | cut -d' ' -f1 | awk -F/ '{ print $NF }')
	    cp $f ${dstfname}.flv
	fi
    done
done

It should be quite clear from the script that this will work only if you use a browser which uses the libflashplayer.so plugin. I think the binary versions of Google Chrome packs in a flashplayer by default, and hence, might not work on it.

How to use this script:

After you’ve finished watching a video, do not close the window. Open the terminal and execute this script; see the example below.
Suppose the video I watched had filename FlashXXXX when cached to your system, then on running the script, you should get FlashXXXX.flv in the directory where you ran the script.

$ cd
$ wget https://raw.github.com/nileshgr/utilities/master/general/flashdownload.sh
$ chmod +x flashdownload.sh
$ ./flashdownload.sh

As stated earlier, you should get FlashXXXX.flv in your home directory (because that’s where the script was run in the example).
Note: This script will save all flash videos (ads too!), so you’ll have to watch whatever gets saved to make sure you got what you want, and then delete the rest.

Tags: , ,
Author: Nilesh Govindrajan Google Profile for Nilesh Govindrajan
Nilesh is a student of computer engineering in Pune who takes crazy interest in Linux and Linux-related fields. He blogs at nileshgr.com Follow him on twitter: @nileshgr

Nilesh Govindrajan has written and can be contacted at nilesh@techie-buzz.com.

Leave a Reply

Name (required)

Website (optional)

 
    Warning: call_user_func() expects parameter 1 to be a valid callback, function 'advanced_comment' not found or invalid function name in /home/keith/techie-buzz.com/htdocs/wp-includes/comment-template.php on line 1694
 
Copyright 2006-2012 Techie Buzz. All Rights Reserved. Our content may not be reproduced on other websites. Content Delivery by MaxCDN