How to programmatically start / stop transcoding a FFMPEG stream

I have an ip webcam that provides MJPEG stream. I can successfully transcode and save this stream using ffmpeg under OSX. The following gives me pretty much what I want:

ffmpeg -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4

This will start the FFMPEG session and start saving the live stream to my test.mp4 file. pressing q will close ffmpeg and save the file.

I would like to programmatically start and stop recording using a PHP or Bash shell script. I tried the following:

<?php

$pid = pcntl_fork();

if($pid == -1){
    die("could not fork"); 
}elseif($pid){
    // we are the parent...
    print $pid.' started recording. waiting 10 seconds...';
    sleep(10); // Wait 10 seconds

    print_r(shell_exec("kill ".$pid)); // Kill the child recording process

    echo 'done';
    exit(); 
}else{
    // we are the child process. call ffmpeg.
    exec('../lib/ffmpeg -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4');
}

But there are two problems:

  • The ffmpeg process does not end / does not die (possibly because it is forked again)
  • When I manually kill the ffmpeg process, the video file cannot be read
+4
3

.

ffmpeg , -y.

ffmpeg -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4

ffmpeg -y -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4 </dev/null >/dev/null 2>/tmp/ffmpeg.log &

, , kill ffmpeg, .

-t ( ) 1 , , ffmpeg 15 1 . 10 25 , , , 14 . php- 30 , .

, PHP ffmpeg, ( ) , , ..

, , ( insteon, , , ).

, PHP- , - :

<?php
print date('H:i:s')."\nStarted recording. waiting 60 seconds...\n";
exec('../lib/ffmpeg -y -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJpeg?Resolution=640x480&Quality=Standard" -b:v 1500k -vcodec libx264 /tmp/test.mp4 </dev/null >/dev/null 2>/tmp/ffmpeg.log &');
sleep(60); // Wait long enough before killing, otherwise the video will be corrupt!

shell_exec('pkill ffmpeg'); // find and kill    
echo "done\n\n";
exit(); 
?>
+3

// "q"! process.StandardInput.WriteLine( "");

0

, . ffmpeg?

#!/bin/bash
ffmpeg -f mjpeg -i "http://user:pass@10.0.1.200/nphMotionJp..../tmp/test.mp4 &

, , - :

#!/bin/bash
osascript -e 'tell application "ffmpeg" to quit'

ffmpeg, , ,

osascript -e 'tell application "ffmpeg" to stop document /tmp/test.mp4"'

osascript -e 'tell application "System Events" to keystroke "q"'
-1

Source: https://habr.com/ru/post/1525062/


All Articles