4

I'm trying to write a Perl script that runs videos in a directory n times on Windows Media Player one after the other.

For some reason, on the 64th video playing, it gets stuck on system(1, @commands).

Right now, the command is system(1, "C:\\Program Files (x86)\\Windows Media Player\\wmplayer", $path); in the following for-loop.

for (my $i = 0; $i < $n; $i++)
{
    # do stuff

    # Play video
    system(1, "C:\\Program Files (x86)\\Windows Media Player\\wmplayer", $path);

    sleep $duration + 1;

    # do stuff
} 

I'm wondering why it keeps stopping at the 64th video (I've run this multiple times and it's always the 64th.) Maybe someone can explain system(1, @commands) better to me? All I know is that it just doesn't wait for @commands to finish before continuing the program...

Thanks a lot!

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
corgichu
  • 2,580
  • 3
  • 32
  • 46
  • btw, $path is the path of the video I want to play. thanks! – corgichu Jul 18 '12 at 00:10
  • What does the first arguement `1` mean? I'd expect that to try to execute a command named `1`, but it does seem to do *something* on Windows. `perldoc -f system` doesn't seem to mention it. Is this documented somewhere? – Keith Thompson Jul 18 '12 at 00:26
  • Yeah.. that's what I'm not sure of myself. I've only seen it on forums.. like here: http://stackoverflow.com/questions/5481986/how-do-you-start-a-background-process-in-perl – corgichu Jul 18 '12 at 00:31
  • See my comments on Annjawn's answer. – Keith Thompson Jul 18 '12 at 00:39

3 Answers3

10

You've reached the maximum number of child processes you can have running simultaneously. Reap those that have completed using waitpid.


Alternatively, if you don't care about the process's exit code, you might have better luck with

system(qq{start /b "" "c:\...\wmplayer" "$path"});
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • would `waitpid` actually wait for the process to finish? because Windows Media Player doesn't terminate until I use "taskkill" or I manually close the window myself. Thanks for the explanation! =] I also found out that VLC player has -way- more command line support so I ended up using that instead and it is working great so far! – corgichu Jul 18 '12 at 16:06
  • Depends on the 2nd argument you pass to it. – ikegami Jul 18 '12 at 16:53
2

Thanks for all the help everybody!

I found out that VLC player has way more command line support than Windows Media Player and that it has exactly what I need:

vlc --play-and-exit $path

I stopped using system(1,@commands) and I'm just using regular old system(@commands) and everything seems to be working well so far! =]

corgichu
  • 2,580
  • 3
  • 32
  • 46
0

The syntax is

system(command,@args)

or

@args=("command","arg1","arg2"); 
system(@args);

In your case the command is 1, not sure what it means. Rather try

system("C:\\Program Files (x86)\\Windows Media Player\\wmplayer.exe", $path);

Here, the command is the .exe (or the binary) which takes the video path(s) as arguments.

UPDATE: Based on comments

system(1,"C:/Program Files (x86)/Windows Media Player/wmplayer.exe", $path);

should work. Play the 64th video normally to check if it works and or reconfigure the sequence of videos and see if its still the 64th video causing problem.

Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77
  • 2
    Actually a first argument of `1` is a Win32-specific feature. It's not mentioned in `perldoc -f system`, but it's in `perldoc perlport`: As an optimization, may not call the command shell specified in `$ENV{PERL5SHELL}`. "`system(1, @args)`" spawns an external process and immediately returns its process designator, without waiting for it to terminate. Return value may be used subsequently in `wait` or `waitpid`. Failure to `spawn()` a subprocess is indicated by setting `$?` to `255 << 8`. – Keith Thompson Jul 18 '12 at 00:38
  • [continued] `$?` is set in a way compatible with Unix (i.e. the exitstatus of the subprocess is obtained by `$? >> 8`, as described in the documentation). – Keith Thompson Jul 18 '12 at 00:39
  • `\ ` and `/` are equivalent here. If one doesn't work, neither will the other. If anything `\ ` would have a better chance of working. Please delete. – ikegami Jul 18 '12 at 01:20
  • \ is used to escape special characters in this case \ itself. So rather to be specific \\ is similar to / in this case, but there is no substantial authenticity of the statement the "one might work better than the other". – Anjan Biswas Jul 18 '12 at 01:51
  • System calls accept both. Command don't always. try `dir /` at the console. – ikegami Jul 18 '12 at 05:32
  • Either way, you're arguing there is no difference, so why suggest the change!? – ikegami Jul 18 '12 at 05:32