Elaheh
Elaheh

Reputation: 13

How to set FFmpeg to send a signal to java code when it's done with its job?

I have a java servlet which waits for an httprequest, when it arrives, it calls the FFmpeg to do mixing two videos. Java code needs to send the mixed video back to the requester when the FFmpeg finished its job. How can I set the FFmpeg to inform the java servlet when the mixed video is ready? My code in current shape, starts sending the video while FFmpeg is not completely done.

I can use thread.sleep() or similar methods, but since we need to measure the processing time for a research work, I cannot use that.

I appreciate if you can help me on this. this is part of the code:

String videoId=req.getParameter("id");         
         String ad= "/var/Videos/ads/angrybirds-adv.mp4";  
         String url="http://"+RequesterIP+"/"+videoId;
         System.out.println("url: "+url);
         String output= new SimpleDateFormat("yyyyMMddhhmm'.mp4'").format(new Date());
         String videoPath = "/var/Videos/"+output;
         List<String> cmds =  new ArrayList<>();
         cmds.add("ffmpeg");
         cmds.add("-i");
         cmds.add(url);
         cmds.add("-i");
         cmds.add(ad);
         cmds.add("-filter_complex");
         cmds.add("[0:v][1:v] overlay");
         cmds.add(videoPath);
         ProcessBuilder pb = new ProcessBuilder(cmds);
         Process p = pb.start();

         /** Terminal **/
       final InputStream inStream = p.getErrorStream();
         new Thread(new Runnable() {
         public void run() {           
                 InputStreamReader reader = new InputStreamReader(inStream);
                 Scanner scan = new Scanner(reader);
                 while (scan.hasNextLine()) {
                     System.out.println(scan.nextLine());
                 }
             }
         }).start();       

// send the mixed video to the requester
File downloadFile = new File(videoPath);
...

More details: the ad video is the overlay video located in the same machine as FFmpeg, the original video is located on a public folder in the requester machine, and is accessible using this url: http://requester-IP/videoId

Upvotes: 0

Views: 380

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86774

You are not waiting for the process to complete, you should be using Process#waitFor() to pause the thread until ffmpeg finishes.

Upvotes: 1

Related Questions