title: Start MPlayer from a PHP command
body: <p>I am working on a project to create a photo booth. I have a camera (canon 550d) and a touchscreen pc (running ubuntu) which shows a webpage I wrote. </p>
  
  <p>I am using <a href="http://gphoto.sourceforge.net/" rel="nofollow">gphoto2</a> to control my DSLR from within the webpage using <code>exec()</code>, I can take photos easily.</p>
  
  <p>My problem is that I want to also show a live preview from the camera before people take the photo. Now, I have a script ready which opens an MPlayer instance within a borderless window which also stays on top of everything, the code is in a file called <code>live.sh</code>:</p>
  
  <pre><code>#!/bin/bash
  gphoto2 --capture-movie --stdout &gt; fifo.mjpg &amp; mplayer -cache 32 -demuxer 35 -noborder -geometry 50%:100 -ontop 1 fifo.mjpg
  </code></pre>
  
  <p>basically gphoto2 streams to fifo.mjpg and then MPlayer plays that file.</p>
  
  <p>It all works fine and well when I run this from the terminal:</p>
  
  <pre><code>bash live.sh
  </code></pre>
  
  <p>It doesn't work if I run the same command from within php</p>
  
  <pre><code>exec("bash live.sh", $output);
  </code></pre>
  
  <p>Take a look at the outputs I get when running from these 2 environments</p>
  
  <p><strong>Running from terminal (THIS WORKS)</strong> :</p>
  
  <pre><code>MPlayer 1.2.1 (Debian), built with gcc-5.3.1 (C) 2000-2016 MPlayer Team
  mplayer: could not connect to socket
  mplayer: No such file or directory
  Failed to open LIRC support. You will not be able to use your remote control.
  
  Playing 1.
  File not found: '1'
  Failed to open 1.
  
  
  Playing fifo.mjpg.
  
  
  libavformat version 56.40.101 (external)
  Capturing preview frames as movie to 'stdout'. Press Ctrl-C to abort.
  
  *** Error ***             
  An error occurred in the io-library ('Could not claim the USB device'):    Could not claim interface 0 (Device or resource busy). Make sure no other program (gvfs-gphoto2-volume-monitor) or kernel module (such as sdc2xx, stv680, spca50x) is using the device and you have read/write access to the device.
  ERROR: Movie capture error... Exiting.
  Movie capture finished (0 frames)
  libavformat file format detected.
  Cannot seek backward in linear streams!
  Seek failed
  [mjpeg @ 0x7f9cffecb540]Found EOI before any SOF, ignoring
  [mjpeg @ 0x7f9cffecb540]Changeing bps to 8
  [lavf] stream 0: video (mjpeg), -vid 0
  VIDEO:  [MJPG]  1056x704  0bpp  25.000 fps    0.0 kbps ( 0.0 kbyte/s)
  Load subtitles in ./
  Failed to open VDPAU backend libvdpau_va_gl.so: cannot open shared object file: No such file or directory
  [vdpau] Error when calling vdp_device_create_x11: 1
  ==========================================================================
  Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
  libavcodec version 56.60.100 (external)
  Selected video codec: [ffmjpeg] vfm: ffmpeg (FFmpeg MJPEG)
  ==========================================================================
  Audio: no sound
  Starting playback...
  [mjpeg @ 0x7f9cffecb540]Found EOI before any SOF, ignoring
  [mjpeg @ 0x7f9cffecb540]Changeing bps to 8
  Could not find matching colorspace - retrying with -vf scale...
  Opening video filter: [scale]
  Movie-Aspect is undefined - no prescaling applied.
  [swscaler @ 0x7f9d00c36fa0]bicubic scaler, from yuv422p to yuyv422 using     MMXEXT
  [swscaler @ 0x7f9d00c36fa0]using unscaled yuv422p -&gt; yuyv422 special converter
  VO: [xv] 1056x704 =&gt; 1056x704 Packed YUY2
  Movie-Aspect is undefined - no prescaling applied.
  VO: [xv] 1056x704 =&gt; 1056x704 Packed YUY2
  V:   0.5   0/  0 37% 14%  0.0% 0 0 50%
  
  MPlayer interrupted by signal 2 in module: enable_cache
  
  
  
  MPlayer interrupted by signal 2 in module: video_read_frame
  [mjpeg @ 0x7f9cffecb540]overread 8
  [mjpeg @ 0x7f9cffecb540]EOI missing, emulating
  V:   0.6   0/  0 36% 13%  0.0% 0 0 50%
  
  Exiting... (Quit)
  </code></pre>
  
  <p>Despite all the errors, everything works surprisingly well.</p>
  
  <p><strong>This is the output I get when running from an exec() in PHP (THIS DOES NOT WORK):</strong></p>
  
  <pre><code>array(16) {
    [0]=&gt;
      string(71) "MPlayer 1.2.1 (Debian), built with gcc-5.3.1 (C) 2000-2016 MPlayer Team"
    [1]=&gt;
      string(39) "Terminal type `unknown' is not defined."
    [2]=&gt;
      string(0) ""
    [3]=&gt;
      string(10) "Playing 1."
    [4]=&gt;
      string(0) ""
    [5]=&gt;
      string(0) ""
    [6]=&gt;
      string(18) "Playing fifo.mjpg."
    [7]=&gt;
      string(29) "
    Cache fill:  0.00% (0 bytes)"
    [8]=&gt;
      string(0) ""
    [9]=&gt;
      string(40) "libavformat version 56.40.101 (external)"
    [10]=&gt;
      string(33) "libavformat file format detected."
    [11]=&gt;
      string(39) "Cannot seek backward in linear streams!"
    [12]=&gt;
      string(42) "[mjpeg @ 0x7fb771bec540]Changeing bps to 8"
    [13]=&gt;
      string(38) "[lavf] stream 0: video (mjpeg), -vid 0"
    [14]=&gt;
      string(69) "VIDEO:  [MJPG]  1056x704  0bpp  25.000 fps    0.0 kbps ( 0.0 kbyte/s)"
    [15]=&gt;
      string(20) "Load subtitles in ./"
  }
  </code></pre>
  
  <p>If anyone has any ideas on how to make this work, I will be very grateful.</p>
  
  <p>Thank you!</p>