Vim Tips Wiki
Register
Advertisement

You can use vim to control vlc and edit song lyrics or audio transcripts: 1. You can add audio timing information to each line of text transcript 2. You can cue vlc audio to the time/line you are editing.

You will need 1. GVIM to edit text 2. PERL to connect gvim to vlc via socket, 3. VLC

Below: modify the path to perl and vim script according to the directory you save them in.

Setup vlc to listen on a socket

"   VLC setup: $ vlc > tools > prefs > main interface > enable RC,
"       Tcp-Input=[localhost:2150]  .. ~/vlc/rc.pl will open this socket.
"       Console: [donot_open] console 

VIM setup

"   VIM Setup:
"     c:\> vim happy.lrc
"     :so %
"     and cline is '[0:23.10] file:///C:/tmp/happy.mp3'
"   Music player key:
"     [F2] Play
"     [F3] Stop
"     [F4] Pause toggle
"   Lyric editor keys:
"     [F9]  Set filename or time from cline to vlc.
"     [F10] Get file from vlc into cline.
"     [F11] Get Time from vlc into cline.   
</nowiki>
== Map your VLC controller keys
<nowiki>
nmap <F2>   :silent !perl ~/vlc/rc.pl -cmd=play<CR>
nmap <F3>   :silent !perl ~/vlc/rc.pl -cmd=stop<CR>
nmap <F4>   :silent !perl ~/vlc/rc.pl -cmd=pause<CR>

nmap <F9>   :call Mosh_Set_File_Time()<CR>
nmap <F10>  :call Mosh_Get_Filename()<CR>
nmap <F11>  :call Mosh_Get_Time()<CR>               

source this file vlc-lrc.vim

" ==============================================================================
" GPL(C) moshahmed
function! Mosh_Set_File_Time()
  " Test [33] file:///c:/tmp/happy.mp3

  " Look for filename on cline
  let l:matcher_file = matchlist(getline('.'),'\v<(file:///.*[.]\w+)>')
  if len(l:matcher_file) > 4
   "echoerr     "!perl ~/vlc/rc.pl -cmd=set_file=" . l:matcher_file[1] 
    exe ":silent !perl ~/vlc/rc.pl -cmd=set_file=" . l:matcher_file[1]
  endif

  " Look for timestamp on cline, eg. [10] for 10 seconds, [1:1] for 61 seconds.
  let l:matcher_time = matchlist(getline('.'),'\v\[(\d.*)\]')
  if len(l:matcher_time) > 0
   "echoerr     "!perl ~/vlc/rc.pl -cmd=set_time=" . l:matcher_time[1]
    exe ":silent !perl ~/vlc/rc.pl -cmd=set_time=" . l:matcher_time[1]
  else
    " Look for timestamp colon without brackets on cline, eg. 1:2 for 62 seconds.
    let l:matcher_time = matchlist(getline('.'),'\v(\d+:[0-9:.]+)')
    if len(l:matcher_time) > 0
     "echoerr     "!perl ~/vlc/rc.pl -cmd=set_time=" . l:matcher_time[1]
      exe ":silent !perl ~/vlc/rc.pl -cmd=set_time=" . l:matcher_time[1]
    endif
  endif

  " If neither file or time on cline, insert them.
  if len(l:matcher_time) < 1 && len(l:matcher_file) < 1
    :call Mosh_Get_Filename()
    :call Mosh_Get_Time()
  endif
endfunction

" ==============================================================================
function! Mosh_Get_Filename()
 "echoerr "!perl ~/vlc/rc.pl -cmd=get_file"
  exe  ":r !perl ~/vlc/rc.pl -cmd=get_file"
endfunction

" ==============================================================================
function! Mosh_Get_Time()
 "echoerr     "system  perl ~/vlc/rc.pl -cmd=get_time"
  let l:timeis=system("perl ~/vlc/rc.pl -cmd=get_time")
  if  len(l:timeis) > 0
    let l:timeis=substitute(l:timeis, '[^0-9:.]', '', 'g')
    let l:timeis='['.l:timeis.']'
    exe ':s/^/'.l:timeis.'/'
  endif
endfunction

" ==============================================================================  

Keep this perl file in your path

#!/usr/bin/perl -w
# was telnet.pl from perlmonks, modified to work with vlc remote control.
# modifed by moshahmed on 2013-08-22
use strict;
use IO::Socket;

my ( $host, $port, $kidpid, $handle, $line );
( $host, $port ) = ('127.0.0.1', 2150);
my ($verbose,$debug,$cmd,$value,$linein) = (0,0,'',,'');

my $USAGE=q{
USAGE: $0 [options] -cmd=VLC_CMDS
WHAT: control vlc from vim
Options:
    -port=NN  .. default ($port)
    -h      .. help.
    -v      .. verbose
    -debug  .. to debug, also use ./telnet.pl to communicate with vlc
VLC_CMDS:
    play, pause, pause (toggle), stop,
    get_length .. in seconds
    get_time   .. in seconds
    get_file   .. gets file:///path
    set_time=1:2:3.40  .. for 1hour 2minutes 3seconds
    set_file=file:///c:/tmp/happy.mp3
};

# Process args
while( $_ = $ARGV[0], defined($_) && m/^-/ ){ shift; last if /^--$/; if(0){
    }elsif( m/^-[h?]$/ ){ die $USAGE;
    }elsif( m/^-v$/    ){ $verbose++;
    }elsif( m/^-debug$/    ){ $debug++;
    }elsif( m/^-port=(\d+)$/ ){ $port = $1;
    }elsif( m/^-cmd=(set_.+)=(.+)$/ ){ $cmd = $1; $value = $2;
      # convert: 1:2:3.4 => 1h.2m.3.4s into seconds.
      if ($cmd eq 'set_time' && $value =~ m,:, ) {
        my ($timeis,$factor)=(0,1);
        while( $value =~ s,^(\d+):,, ) {
          $factor *= 60;
          $timeis += $factor * $1;
        } 
        $timeis += $value;
        $value = int($timeis);
      }
      die "debug: -cmd=$cmd=$value\n" if $debug;
    }elsif( m/^-cmd=(.+)$/ ){ $cmd = $1;
    }elsif( m/^-linein$/ ){ $linein=1;
    }else{ die $USAGE,"Unknown option '$_'\n"; }
}

# create a tcp connection to the specified host and port
$handle = IO::Socket::INET->new(
    Proto    => "tcp", PeerAddr => $host, PeerPort => $port
  ) or die "can't connect to port $port on $host: $!";
  $handle->autoflush(1);    # so output gets there right away
print STDERR "[Connected to $host:$port]\n" if $verbose;

# split the program into two processes, identical twins
die "can't fork: $!" unless defined( $kidpid = fork() );

if ($kidpid) { # In parent, parse reply from vlc.
    # the if{} part runs only in the parent process
    DONE:
    while ( defined( $line = <$handle> ) ) {
      if    ($cmd eq 'get_length') { 
        if( $line =~ m,^\d+,) {
          print STDOUT $line;
          last DONE;
        }
      }elsif($cmd eq 'get_time') {
        if( $line =~ m,^\d+,) {
          print STDOUT $line;
          last DONE;
        }
      }elsif($cmd eq 'get_file') {
        if( $line =~ m,(file://.*)\s[()],i ) {
          my $filename = $1;
          print STDOUT $filename;
          last DONE;
        }
      }elsif($cmd eq 'status') {
        print STDOUT $line;
        last DONE if $line =~ m,returned,;
      }elsif($cmd eq 'play') { last DONE;
      }elsif($cmd eq 'pause') { last DONE;
      }elsif($cmd eq 'stop') { last DONE;
      }else{ print STDOUT $line;
        last DONE if $line =~ m,returned,;
      }
    } # DONE
    kill( "TERM", $kidpid );    # send SIGTERM to child
    exit 0;
} else {  # In child? send cmd to vlc
    if(     $cmd eq 'status' ){ print $handle "$cmd\n";
    }elsif( $cmd eq 'play' ){ print $handle "$cmd\n";
    }elsif( $cmd eq 'pause' ){ print $handle "$cmd\n";
    }elsif( $cmd eq 'stop' ){ print $handle "$cmd\n";
    }elsif( $cmd eq 'get_time' ){ print $handle "$cmd\n";
    }elsif( $cmd eq 'get_file' ){ print $handle
        "pause\nstatus\npause\nstatus\n";
    }elsif( $cmd eq 'get_length' ){ print $handle "play\nget_length\n";
    }elsif( $cmd eq 'set_time' ){ print $handle "seek $value\n";
    }elsif( $cmd eq 'set_file' ){ print $handle "clear\nadd $value\n";
    }else{                        print $handle "$cmd\n";
    }
    exit 0;
}  
Advertisement