Videogrep

From ActiveArchives
Jump to: navigation, search

Contents

Download

The code described on this page is available via subversion:

svn co http://activearchives.org/svn/trunk/videogrep

A version created using MLT can be found here.

Description

Videogrep was a "proof of concept" script to demonstrate how a subtitles file could be used as a means of editing (aka "my first and last awk script" -- Murtaugh).

Note that these scripts are not perfect in a number of ways -- one is that they use the microdvd format (see below), and are as a result hardcoded to a particular framerate... A better script would use a more sensible timecode based format, like SRT, but this would be less simple, and the original idea was to make a conceptually simple translation of subtitles to an EDL.

Source

#!/bin/bash
# videogrep
# Usage:
#   ./videogrep <movie_file> <search_term> (optional: <subtitle_to_display>)
#
# The subtitle file to be searched is implicit--it is always named the same 
# as the movie file, but with a .sub extension. We convert automatically 
# to .sub when another format (right now only .srt) is used.
#
# The optional <subtitle_to_display> argument allows a separate .sub file
# to be displayed, such that a user can search in one language .sub and
# display a separate (for instance, search for English and display Spanish).
 
framerate=15
 
vid=$1
term=$2
base=${vid%%.*}
 
if [ ! -e $base.sub ]
then
  if [ -e $base.srt ]
  then
    echo "Dumping $base.sub from $base.srt..."
    mplayer "$vid" -sub "$base.srt" -dumpmicrodvdsub -endpos 1
    mv dumpsub.sub "$base.sub"
  fi
fi
 
subtitle=${vid%%.*}.sub		# look for subtitles as (moviename - ext) + ".sub"
 
dstart=-0.5	# seconds to add to the start time (negative = earlier)
dend=2		# seconds to add to the end time
 
echo searching \'$vid\' for \'$term\' using \'$subtitle\'...
 
cat $subtitle  | \
egrep -i $2 | \
sed -e 's/{\([[:digit:]]*\)}{\([[:digit:]]*\)}.*/\1 \2/' | \
awk '{ print ($1/'$framerate') " " ($2/'$framerate') }' | \
awk 'BEGIN { cur=0 }
{
if ($1 > cur) print cur " " $1 " 0"
cur = $2
}
END { print cur " 10000 0" }
' > videogrep.edl
 
showsub=${3:-$subtitle}
mplayer -sub "$showsub" -edl videogrep.edl -fs "$vid"

History

First version of script

#!/bin/sh
# videogrep
 
framerate=15
 
vid=$1
term=$2
subtitle=${vid%%.*}.sub		# look for subtitles as (moviename - ext) + ".sub"
dstart=-0.5	# seconds to add to the start time (negative = earlier)
dend=0.5		# seconds to add to the end time
 
echo searching \'$vid\' for \'$term\' using \'$subtitle\'...
 
cat $subtitle  | \
grep -i $2 | \
sed -e 's/{\([[:digit:]]*\)}{\([[:digit:]]*\)}.*/\1 \2/' | \
awk '{ print ($1/'$framerate') " " (($2-$1)/'$framerate') }' | \
awk '{ printf("mplayer -fs '$vid' -ss %0.f -frames %0.f < /dev/null\n", ($1 + '$dstart'), int(($2 - '$dstart' + '$dend')*'$framerate')) }' | \
sh >& /dev/null

Second version, producing an "edl" file, resulting in a seemless playback in mplayer.

#!/bin/sh
# videogrep
 
framerate=15
 
vid=$1
term=$2
subtitle=${vid%%.*}.sub		# look for subtitles as (moviename - ext) + ".sub"
 
dstart=-0.5	# seconds to add to the start time (negative = earlier)
dend=2		# seconds to add to the end time
 
echo searching \'$vid\' for \'$term\' using \'$subtitle\'...
 
cat $subtitle  | \
egrep -i $2 | \
sed -e 's/{\([[:digit:]]*\)}{\([[:digit:]]*\)}.*/\1 \2/' | \
awk '{ print ($1/'$framerate') " " ($2/'$framerate') }' | \
awk 'BEGIN { cur=0 }
{
if ($1 > cur) print cur " " $1 " 0"
cur = $2
}
END { print cur " 10000 0" }
' > videogrep.edl
 
showsub=${3:-$subtitle}
mplayer -sub "$showsub" -edl videogrep.edl -fs "$vid"

Usage:

./videogrep_edl mymovie.avi "searchterm"
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox