shellutils

annotate lsflv @ 0:80fc0c4b11c0

collection of shell scripts
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Apr 2015 13:09:47 +0300
parents
children
rev   line source
nuclear@0 1 #!/bin/sh
nuclear@0 2 # -------------------------------------------------------------------------
nuclear@0 3 # lsflv - prints a list (and optionally saves copies of) videos currently
nuclear@0 4 # being played by the adobe flash player browser plugin.
nuclear@0 5 #
nuclear@0 6 # Written by John Tsiombikas <nuclear@member.fsf.org>
nuclear@0 7 # This script is placed in the public domain, do whatever you want with it.
nuclear@0 8 # -------------------------------------------------------------------------
nuclear@0 9
nuclear@0 10 mkcopy=no
nuclear@0 11 play=no
nuclear@0 12 player=${PLAYER:-mpv}
nuclear@0 13 bin=`basename $0`
nuclear@0 14 dir=`pwd`
nuclear@0 15
nuclear@0 16 # if invoked as cpflv always make a copy (as in -c)
nuclear@0 17 if [ "$bin" = cpflv ]; then
nuclear@0 18 mkcopy=yes
nuclear@0 19 fi
nuclear@0 20
nuclear@0 21 # if invoked as playflv play the video (as in -p)
nuclear@0 22 if [ "$bin" = playflv ]; then
nuclear@0 23 play=yes
nuclear@0 24 fi
nuclear@0 25
nuclear@0 26 # process arguments
nuclear@0 27 while [ $# -gt 0 ]; do
nuclear@0 28 case "$1" in
nuclear@0 29 -c)
nuclear@0 30 mkcopy=yes
nuclear@0 31 ;;
nuclear@0 32 -p)
nuclear@0 33 play=yes
nuclear@0 34 ;;
nuclear@0 35 -d)
nuclear@0 36 dir=$2
nuclear@0 37 shift
nuclear@0 38 ;;
nuclear@0 39 -h)
nuclear@0 40 echo "Usage: $bin [options]"
nuclear@0 41 echo "Options:"
nuclear@0 42 echo " -c make copies of the video files"
nuclear@0 43 echo " -d <dir> change the directory used to copy the files to (default: cwd)"
nuclear@0 44 echo " -h print usage and exit"
nuclear@0 45 exit 0
nuclear@0 46 ;;
nuclear@0 47 esac
nuclear@0 48 shift
nuclear@0 49 done
nuclear@0 50
nuclear@0 51
nuclear@0 52 list=`lsof | egrep '(/tmp/Flash|/tmp/mozilla-media-cache)' | awk '{ print $2":"$4 }'`
nuclear@0 53
nuclear@0 54 num=0
nuclear@0 55
nuclear@0 56 for i in $list; do
nuclear@0 57 pid=`echo $i | awk -F: '{ print $1 }'`
nuclear@0 58 fd=`echo $i | awk -F: '{ print $2 }' | sed 's/[a-zA-Z]*//g'`
nuclear@0 59
nuclear@0 60 if [ -n "$fd" ]; then
nuclear@0 61 path="/proc/$pid/fd/$fd"
nuclear@0 62
nuclear@0 63 num=`echo "$num + 1" | bc`
nuclear@0 64 target="$dir/vid$num"
nuclear@0 65
nuclear@0 66 if [ $mkcopy = yes ]; then
nuclear@0 67 echo "copy $path -> $target"
nuclear@0 68 if ! cp $path $target; then
nuclear@0 69 echo "failed to copy $path to $target" >&2
nuclear@0 70 exit 1
nuclear@0 71 fi
nuclear@0 72 else
nuclear@0 73 echo "$path"
nuclear@0 74 fi
nuclear@0 75
nuclear@0 76 if [ $play = yes ]; then
nuclear@0 77 $player $path
nuclear@0 78 fi
nuclear@0 79 fi
nuclear@0 80 done