VLC HowTo/Use for transcription (Linux)

From VideoLAN Wiki
Revision as of 07:48, 6 October 2010 by Derek (talk | contribs) (more formatting issues)
Jump to navigation Jump to search

OVERVIEW:

VLC makes a good option for people trying to do transcription of audio or video in linux. The script below might be usable in other operating systems, but will likely take some adjusting.


The purpose of this howto is to show you how to configure vlc and use the script below to enable you to control vlc with scripted commands and, most importantly, take a timestamp from vlc and automatically type it into whatever document you are working on, without ever needing to shift focus away from the document.


There are other ways to achieve what I've done with this, many of them likely better than what is here. But this works, and works well.


PREREQUISITES:

First, you need to have VLC (obviously), xautomation OR a version of xdotool (newer than august 2010), and you need the openbsd implementation of netcat.


The openbsd version of netcat is in the repositories of most major linux distirbutions these days. This script is written for use in Archlinux, so it's likely that you will need to change the command used by netcat in the script. Arch uses nc.openbsd, just replace it with whatever you need.


xautomation will almost certainly be in your repos, and xdotool might be. xautomation seems to be the better choice if you're only concerned about how quickly it does the job, but xdotool is nice as well if you're doing fancier things with it. However, if you do choose to try xdotool, you'll need a build done later than August 2010, as some of the features used in this script are not in the older versions available in most repositories.


SETUP:

To set up vlc to use this script, go to tools->preferences and click on "show settings->all" at the bottom. From that menu, select "Interface->Main Interfaces", and check the "Remote Control Interface" box. Next, select "Interface->Main Interfaces->RC", check the "Fake TTY' box, and enter 'home/YOURNAME/vlc.sock'in the "UNIX socket command input" field.


You probably also want to adjust the "Very short jump length" located in "Interface->Hotkeys settings". This script assumes that it is set for 5 seconds rather than the default of 3 seconds. It won't affect the script if you don't change this value, as it uses the 'very short jump' command rather than jogging a specific number of seconds. If you poke around the vlc docs, you'll see a seek command, but that is to go to a certain point in a file rather than going forward or backward a certain number of seconds.


Hit "Save". Restart VLC, and check to see if it creates "vlc.sock" in your home directory. This should be created automatically when vlc starts. If it doesn't, check your socket path and try again.


Next, you need to set up your hotkeys for your window environment. This should work equally well in any window manger, so pick whichever you like. Remember to check to make sure that whichever hotkeys you wish to use are not already used by your windowmanager. Redefine these hotkeys or the defaults as necessary. I didn't have any luck with the global hotkeys settings within VLC, but I personally like having these commands in the script, anyway. You may have better results with some experimentation.


#!/usr/bin/env python
#### licensing nonsense - short version: New BSD License
#Copyright (c) 2010, Derek Barnett, Skyehaven Transcription
#Contact: derek@skyehaven.net
#All rights reserved.
#Redistribution and use in source and binary forms, with or 
#without modification, are permitted provided that the following 
#conditions are met:
#
#    * Redistributions of source code must retain the above 
#copyright notice, this list of conditions and the following 
#disclaimer.
#    * Redistributions in binary form must reproduce the above 
#copyright notice, this list of conditions and the following 
#disclaimer in the documentation and/or other materials provided 
#with the distribution.
#    * Neither the name of the Skyehaven Transcription nor the 
#names of its contributors may be used to endorse or promote 
#products derived from this software without specific prior 
#written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
#CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
#INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
#MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
#CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
#SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
#LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
#USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
#AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
#LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
#IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
#THE POSSIBILITY OF SUCH DAMAGE.
#####

##### useful notes
#vlc control script, compatible with python 2 or 3
#
#this script requires the openbsd version of netcat. gnu's version
#does not appear to support unix sockets as of this time. it will 
#almost certainly be in the repos of your distribution. you may need
#to adjust this script to change the name of the binary to whatever
#format your distribution uses (change 'nc.openbsd' below to whatever 
#you need.)
#
#it also requires either xautomation (for it's xte tool) or xdotool
#xautomation is used by default, as it's currently a bit faster
#if you use xdotool, ensure that it's a version later than aug 2010
#
#to set up vlc to use this script, go to tools->preferences and 
#click on "show settings->all" at the bottom. from that menu, 
#select "Interface->Main Interfaces", and check the "Remote Control 
#Interface" box. Next, select "Interface->Main Interfaces->RC",
#check the "Fake TTY' box, and enter 'home/YOURNAME/vlc.sock'in
#the "UNIX socket command input" field.  
#
#You probably also want to adjust the "Very short jump length" located
#in "Interface->Hotkeys settings". This script assumes that it is set
#for 5 seconds rather than the default of 3 seconds. It won't affect the
#script if you don't change this value, as it uses the 'very short jump'
#command rather than jogging a specific number of seconds. If you poke 
#around the vlc docs, you'll see a seek command, but that is to go to a
#certain point in a file rather than going forward or backward a certain
#number of seconds.
#
#Hit "Save". Restart VLC, and check to see if it creates "vlc.sock" 
#in your home directory. This should be created automatically when vlc 
#starts. If it doesn't, check your socket path and try again.
#
#Next, you need to set up your hotkeys for your window environment.
#This should work equally well in any window manger, so pick whichever
#you like. Remember to check to make sure that whichever hotkeys you wish
#to use are not already used by your windowmanager. Redefine these
#hotkeys or the defaults as necessary.
#
#note: vlc supposedly support global hotkeys, but I didn't have any luck 
#with them, which is why i went this route with the control script. Your
#mileage may vary.
#
#I personally set it up like this:
#F1 = ~/vlccontrol.py jogbackward
#F2 = ~/vlccontrol.py pause (no need to have this twice, just habit)
#F3 = ~/vlccontrol.py pause
#F4 = ~/vlccontrol.py jogforward
#F5 = ~/vlccontrol.py timestamp
#Shift+F1 = ~/vlccontrol.py slower
#Shift+F3 = ~/vlccontrol.py normal
#Shift+F4 = ~/vlccontrol.py faster
##### end of rambling, on to business

import sys
import os

#feed command to vlc socket to get the time played in seconds
workingdir = os.path.join(os.path.expanduser('~'))
vlcin = os.path.join(workingdir,'vlc.sock')
vlcout = os.path.join(workingdir,'vlc.out')

#accept argument when running script, e.g. './vlctimestamp.py timestamp'
args = sys.argv[1:]
i = "normal"
if args:
    i = str.lower(args[0])

#acceptable arguments: help, --help, pause, jogforward, +5, jogbackward, -5,
#faster, slower, normal, timestamp. no argument assumes 'normal'
if i == "help" or i == "-help" or i == "--help":
    print("""
             'help' or '--help' returns this help
             'pause' is a play/pause toggle
             'jogforward' or '+5' jumps forward 5 seconds
             'jogbackward' or '-5' jumps backward 5 seconds
             'faster' increases the tempo without increasing pitch
             'slower' decreases the tempo without decreasing pitch
              no argument or 'normal' returns vlc to normal speed
              'timestamp' types a hh:mm:ss coded timestamp into 
                          active window. see comments within this
                          script if you need to change the timestamp
                          string, offset the timestamp for a video
                          timecode, or if you've made tempo changes
                          in an audio file outside of vlc
             """)

elif i == "jogforward" or i == "+5":
    os.system('echo "key key-jump+extrashort" | nc.openbsd -U ' + vlcin)

elif i == "jogbackward" or i == "-5":
    os.system('echo "key key-jump-extrashort" | nc.openbsd -U ' + vlcin)

elif i == "pause":
    os.system('echo "pause" | nc.openbsd -U ' + vlcin)

elif i == "faster":
    os.system('echo "key key-rate-faster-fine" | nc.openbsd -U ' + vlcin)

elif i == "slower":
    os.system('echo "key key-rate-slower-fine" | nc.openbsd -U ' + vlcin)

elif i == "normal":
    os.system('echo "normal" | nc.openbsd -U ' + vlcin)

elif i == "timestamp":
    #have vlc post the time ~/vlc.out
    os.system('echo "get_time" | nc.openbsd -U ' + vlcin + ' > ' + vlcout)
    
    #read vlc.out and report time played in seconds
    f = open(vlcout, 'r')
    f_list = f.read().split("\n")
    if len(f_list) > 2:
        sec = f_list[1]
    else:
        sec = f_list[0]
    sec = int(sec)
    
    #tempo - if you've adjusted the tempo and an audio file, in 
    #        audacity for instance, then you can use the tempo
    #        variable to give output for a timestamp postion in 
    #        original file. tempo is the percent playback speed
    #        of the modified file. 80 = -20% tempo change, etc.
    #        default is 100
    tempo = 100
    
    #change offsetneeded to True if, for instance, you need to 
    #use a timecode embedded into a video rather than the playtime
    #of the file
    offset = 0
    offsetneeded = False
    if offsetneeded == True:
    
    #If an offset is needed:
    #Pick a spot on the video and pause it. Enter the appropriate values below:
    #vtch = hours on video time code, vtcm = minutes, vtcs = seconds
        vtch = 0
        vtcm = 0
        vtcs = 0
        vtc = (vtch * 3600) + (vtcm * 60) + vtcs
    #atch = hours in actual playtime, atcm = minutes, 
    #atcs = seconds            
        atch = 0
        atcm = 0
        atcs = 0
        atc = ((((atch * 3600) + (atcm * 60) + atcs) * tempo) / 100)
        offset = vtc - atc
    
    #get the values for hh:mm:ss formatting
    sec = ((sec * tempo) / 100) + offset
    th = sec/3600
    tm = (sec % 3600)/60
    ts = sec % 60
    
    #format the timestamp, default looks like '##Inaudible 00:01:10## '
    #the timestamp in hours:minutes:seconds                    
    t = "%02d:%02d:%02d" % (th,tm,ts)    
    
    #string to append before timestamp
    #for no prefix, set prefix = ""
    prefix = "##Inaudible "
    #string to append after timestamp
    #for no suffix, set suffix = ""
    suffix = "## "                           
        
    #xdotool command to execute, uncomment next line to use xdotool
    #dropstamp = str("xdotool type --delay 0 --clearmodifiers '" + prefix + t + suffix + "'")
    #    
    #drop the timestamp string into active window, uncomment next line to use xdotool
    #os.system(dropstamp)
    #        
    #use xte from the xautomation package if you don't have a version of 
    #xdotool newer than august 2010
    os.system('xte "str ' + prefix + t + suffix + '"')       

#if we don't feed an argument to the script, normalize the play speed of vlc
else:
    os.system('echo "normal" | nc.openbsd -U ' + vlcin)