Sunday, January 8, 2012

Shell script to save Flash video on Linux
Any from /tmp/Flash* including those (deleted) but still open


FLASHPIDS=`lsof | grep -i /tmp/Flash | awk '{ print $2 }' | head -n 1`

echo "$FLASHPIDS" | while read FLASHPID; do
  if [ -n "$FLASHPID" ]; then
    cd "/proc/$FLASHPID/fd"
    ls -l | grep -i /tmp/Flash | awk '{ print $9 }' | while read FDNUM; do
      if cp "-i" "$FDNUM" "$TARGETFILE"; then
        echo "Created $TARGETFILE"
    echo "lsof returned 0 processes that have open files with /tmp/Flash in the name. Exiting..."
All open videos will be copied even if incomplete, so wait until they fully load in your web browser before running this script!

Newer versions of Flash have been deleting cached flv videos from the usual /tmp/FlashXX?????? location, while still holding them open. They can still be accessed through /proc/$FLASHPID/fd/$FDNUM though, and that is how this script copies them to the TARGETFOLDER.

Files are saved with with their names in the format $PIDOFSCRIPT-$FLASHPID-$FDNUM.flv, and it is up to you to rename them. $PIDOFSCRIPT is $$ in bash, and is used to reduce the likelihood of colliding with older video names. The same $FDNUM and $FLASHPID combination often appear multiple times within the same browsing session for different videos, as older $FDNUM values from previously closed videos are reused. In the unlikely event of a collision cp -i is used so that a warning is issued.