VLC automation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajit Teli
    New Member
    • Jan 2011
    • 4

    VLC automation

    I have pasted the perl script below. Here, I have stored audio files into the mysql database. the "print `vlc "@row"`;" statement launches vlc and starts playing. But it is not quitting after sleeping fot 10 sec. What do i need to do for that?

    Code:
    #!usr/bin/perl
    use strict;
    no strict "vars";
    use warnings;
    
    use DBI;
    $dbh = DBI->connect('dbi:mysql:audio', 'root', 'ajitteli')
    	or die "connection error: $DBI::errstr\n";
    
    print "Enter the number of songs to be played:  ";
    my $num = <stdin>;
    
    for (my $i=1; $i<=$num; $i++) {
    	$sql = "select file_path from audio_table where id like $i";
    	$sth = $dbh->prepare($sql);
    	$sth->execute
    		or die "SQL Error: $DBI::errstr\n";
    
    	#use IPC::open3;
    
    	# my %videoInfo = videoInfo($sth);
    
    	# $durtn = $videoInfo{'durationsecs'};
    
    
    	while (@row =$sth->fetchrow_array) {
    		print "\n\nPlaying song $i\n\n";
    
    		print `vlc "@row"`;
    		print "song$i stopped\n";
    		sleep 10;
    
    		print `vlc://quit`;
    	}
    Last edited by miller; Apr 1 '11, 06:21 AM. Reason: Code Tags
  • Ajit Teli
    New Member
    • Jan 2011
    • 4

    #2
    I am able to

    Here vlc://quit is not working. What do i need to for quitting vlc through perl?

    Code:
    #!usr/bin/perl
    use strict;
    no strict "vars";
    use warnings;
    
    use DBI;
    $dbh = DBI->connect('dbi:mysql:audio', 'root', 'ajitteli')
    	or die "connection error: $DBI::errstr\n";
    
    print "Enter the number of songs to be played:  ";
    my $num = <stdin>;
    
    for (my $i=1; $i<=$num; $i++) {
    	$sql = "select file_path from audio_table where id like $i";
    	$sth = $dbh->prepare($sql);
    	$sth->execute
    		or die "SQL Error: $DBI::errstr\n";
    
    	while (@row =$sth->fetchrow_array) {
    		print "\n\nPlaying song $i\n\n";
    
    		print `vlc "@row"`;
    
    		print "song$i stopped\n";
    		sleep 10;
    
    		print `vlc://quit`;
    	}
    Last edited by miller; Apr 3 '11, 04:11 AM. Reason: Please do not double post nor post Questions in the Insights section of the forums. I've merged your threads and added [code] tags to both of your posts.

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      I do not believe that you understand the nature of backticks ``. Read perlop, search for "qx/STRING/".

      Backticks execute an external program but they will wait until it's done before returning the results. If you want to execute vlc.exe and not wait for it to end, you'll have to fork like the following

      Code:
      #!/usr/bin/perl
      
      use Cwd qw(chdir);
      
      use strict;
      use warnings;
      
      my $dir = q{C:\Program Files\VideoLAN\VLC};
      my $vlc = q{vlc.exe};
      my $mp3 = q{C:\Dropbox\devel\intro.mp3};
      
      chdir($dir);
      
      my $pid = fork();
      
      if (not defined $pid) {
          warn "resources not avilable.\n";
      
      } elsif ($pid == 0) {
      	exec $vlc, $mp3;
          exit(0);
      }
      
      sleep 10;
      
      # attempt to kill vlc
      
      waitpid($pid,0);
      
      1;
      
      __END__
      Now how to kill vlc, I don't know. You'll have to determine the process id of it somehow and send the kill request.

      - Miller

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        Here is how you would kill each vlc instance on windows:

        Code:
        #!/usr/bin/perl
        
        use Cwd qw(chdir);
        use Win32::Process::List;
        
        use strict;
        use warnings;
        
        my $dir = q{C:\Program Files\VideoLAN\VLC};
        my $vlc = q{vlc.exe};
        my $mp3 = q{C:\Dropbox\devel\intro.mp3};
        
        chdir($dir);
        
        my $pid = fork();
        
        if (not defined $pid) {
            warn "resources not avilable.\n";
        
        } elsif ($pid == 0) {
        	exec $vlc, $mp3;
            exit(0);
        }
        END {
        	waitpid($pid, 0);
        }
        
        
        for (1..5) {
        	print "Hello $_\n";
        	sleep 1;
        }
        
        my $P = Win32::Process::List->new();
        my %list = $P->GetProcesses();
        if (my @vlc_pids = grep {$list{$_} eq $vlc} keys %list) {
        	print "@vlc_pids\n";
        	kill 9, $_ for (@vlc_pids);
        }

        Comment

        • RonB
          Recognized Expert Contributor
          • Jun 2009
          • 589

          #5
          I'd use Win32::Process which provides methods for creating, suspending, and killing to name a few.

          I don't have time to write an example, but the documentation is pretty clear.

          Comment

          Working...