关闭应用程序JAVA后ffmpeg工作

时间:2013-06-23 00:17:05

标签: java ffmpeg

我是布尔汉。 我尝试用ffmpg分割视频到帧,但我遇到了问题。在关闭我的应用程序之前,图片没有显示在文件夹中。这是我的代码:

public void SplitVideo(String lokasi) {
try {
    //excecute cmd
    String cmd = "c:\\ff\\bin\\ffmpeg.exe -i " + lokasi + " -y -f image2 c:\\vid\\img-%07d.jpg";
    Process jalan = Runtime.getRuntime().exec(cmd);
    // Get output stream to write from it
    OutputStream keluaran = jalan.getOutputStream();
    System.out.println(keluaran);
} catch (IOException e) {
    System.out.println(e);
}}

我正在使用java。 请你告诉我我的问题。

2 个答案:

答案 0 :(得分:1)

我使用FFmpeg将我的网络摄像头和语音流式传输到另一台计算机。我遇到了同样的问题,所以我使用Thread来执行FFmpeg,并使用了一个执行kill cmd的类来停止FFmpeg。

PS: 文件已从给定的URL中删除,因此我编辑此帖子以放入我的代码

package com.pfe.config;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;



public class FFmpeg {

private  String ip="";
private  int port=0;
private  String videoDevice="";
private  String audioDevice="";
private  List<String>devices=null; 


public FFmpeg() {

}

public FFmpeg(String ip, int port) {
    super();
    this.ip = ip;
    this.port = port;
}

public FFmpeg(String ip, int port, String videoDevice, String audioDevice) {
    this.ip = ip;
    this.port = port;
    this.videoDevice = videoDevice;
    this.audioDevice = audioDevice;
}

public void executeFFmpeg() throws Exception {
    String commande = "ffmpeg -f dshow -i video=^\""+videoDevice+"^\":audio=^\""+audioDevice+"^\" -vcodec libx264 -preset ultrafast -tune zerolatency -r 30 -async 1 -acodec libmp3lame -ab 24k -ar 22050 -bsf:v h264_mp4toannexb -maxrate 750k -bufsize 3000k  -f mpegts udp://"+ip+":"+port;
    System.out.println(commande);
    ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", commande.toString());
    builder.redirectErrorStream(true);
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (true) {
        line = r.readLine();
        if (line == null) { 
            break; 
        }
        System.out.println(line);
    }

}

public void executeFFmpeg(String ip, int port, String videoDevice, String audioDevice) throws Exception {
    String commande = "ffmpeg -f dshow -i video=^\""+videoDevice+"^\":audio=^\""+audioDevice+"^\" -vcodec libx264 -preset ultrafast -tune zerolatency -r 30 -async 1 -acodec libmp3lame -ab 24k -ar 22050 -bsf:v h264_mp4toannexb -maxrate 750k -bufsize 3000k  -f mpegts udp://"+ip+":"+port;
    System.out.println(commande);
    ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", commande.toString());
    builder.redirectErrorStream(true);
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (true) {

        line = r.readLine();
        if (line == null) { 

            break; 
        }
        System.out.println(line);
    }

}

public List<String> listDiveces() throws IOException{

    String commande = "ffmpeg -list_devices true -f dshow -i dummy";
    ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", commande.toString());
    builder.redirectErrorStream(true);
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    List<String> ds = new ArrayList<String>();
    List<String> ds1 = new ArrayList<String>();
    while (true) {

        line = r.readLine();
        if (line == null) { 

            break; 
        }
        if(line.contains("dshow")){
            ds.add(line);
        }
    }
    if(!ds.isEmpty()){

        StringTokenizer st = new  StringTokenizer("");
        for(int i=0;i<ds.size();i++){

            st = new StringTokenizer(ds.get(i) ,"]");

            while (st.hasMoreTokens()){

                ds1.add(st.nextToken());

            }

        }
        ds.clear();
        for(int i=0;i<ds1.size();i++){

            if(!ds1.get(i).contains("[")&&ds1.get(i).contains("\"")){

                ds.add(ds1.get(i)); 
            }       
        }
        for(int i=0;i<ds.size();i++){

            System.out.println(ds.get(i));
            ds.set( i, ds.get(i).replaceAll("  ", ""));
            ds.set(i,ds.get(i).replaceAll("\"", ""));
            System.out.println(ds.get(i));

        }
    }
    return ds;
}

public void executeFFplay(String ip,int port) throws IOException {
    String commande = "ffplay udp://"+ip+":"+port;
    ProcessBuilder builder = new ProcessBuilder(
        "cmd.exe", "/c", commande.toString());
    builder.redirectErrorStream(true);
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (true) {
        line = r.readLine();
        if (line == null) { break; }
        System.out.println(line);
    }
}

public void executeFFplay() throws IOException {
    String commande = "ffplay udp://"+ip+":"+port;
    ProcessBuilder builder = new ProcessBuilder(
        "cmd.exe", "/c", commande.toString());
    builder.redirectErrorStream(true);
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (true) {
        line = r.readLine();
        if (line == null) { break; }
        System.out.println(line);
    }
}

public String getIp() {
    return ip;
}

public void setIp(String ip) {
    this.ip = ip;
}

public String getVideoDevice() {
    return videoDevice;
}

public void setVideoDevice(String videoDevice) {
    this.videoDevice = videoDevice;
}

public String getAudioDevice() {
    return audioDevice;
}

public void setAudioDevice(String audioDevice) {
    this.audioDevice = audioDevice;
}

public int getPort() {
    return port;
}

public void setPort(int port) {
    this.port = port;
}

public List<String> getDevices() {
    return devices;
}

public void setDevices(List<String> devices) {
    this.devices = devices;
}  }


*************************************************************

package com.pfe.config;


public class FFmpegThread implements Runnable {

private  FFmpeg fFmpeg;
private  String ip;
private  int port;
private  String videoDevice;
private  String audioDevice;

public FFmpegThread(FFmpeg fFmpeg) {

    this.fFmpeg = fFmpeg;

}
public FFmpegThread( String ip, int port, String videoDevice, String audioDevice) {

    this.ip = ip;
    this.port = port;
    this.videoDevice = videoDevice;
    this.audioDevice = audioDevice;
    this.fFmpeg = new FFmpeg(ip, port, videoDevice, audioDevice);
}
@Override
public void run() {

    try {
        fFmpeg.executeFFmpeg();
    }
    catch (Exception e) {

        e.printStackTrace();
    }


}
public FFmpeg getfFmpeg() {
    return fFmpeg;
}
public void setfFmpeg(FFmpeg fFmpeg) {
    this.fFmpeg = fFmpeg;
}
public String getIp() {
    return ip;
}
public void setIp(String ip) {
    this.ip = ip;
}
public int getPort() {
    return port;
}
public void setPort(int port) {
    this.port = port;
}
public String getVideoDevice() {
    return videoDevice;
}
public void setVideoDevice(String videoDevice) {
    this.videoDevice = videoDevice;
}
public String getAudioDevice() {
    return audioDevice;
}
public void setAudioDevice(String audioDevice) {
    this.audioDevice = audioDevice;
}}

package com.pfe.config;
import java.io.IOException;

public class FFPlayThread implements Runnable {

private  String ip;
private  int port;
private  FFmpeg fFmpeg;


public FFPlayThread(FFmpeg fFmpeg) {
    this.fFmpeg = fFmpeg;
}


public FFPlayThread(String ip, int port) {
    this.ip = ip;
    this.port = port;
    this.fFmpeg = new FFmpeg(ip, port);
}


@Override
public void run() {

    try {
        fFmpeg.executeFFplay();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


public String getIp() {
    return ip;
}


public void setIp(String ip) {
    this.ip = ip;
}


public int getPort() {
    return port;
}


public void setPort(int port) {
    this.port = port;
}


public FFmpeg getfFmpeg() {
    return fFmpeg;
}


public void setfFmpeg(FFmpeg fFmpeg) {
    this.fFmpeg = fFmpeg;
}}

package com.pfe.config;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Kill {

    private static final String TASKLIST = "tasklist";
    private static final String KILL = "taskkill /F /IM ";

    public static boolean isProcessRunging(String serviceName) throws Exception {

        Process p = Runtime.getRuntime().exec(TASKLIST);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {

            //System.out.println(line);
            if (line.contains(serviceName)) {
                return true;
            }
        }

        return false;

    }

    public static void killProcess(String serviceName) throws Exception {

        Runtime.getRuntime().exec(KILL + serviceName);

    }}

import java.util.List;

import com.pfe.config.FFPlayThread;
import com.pfe.config.FFmpeg;
import com.pfe.config.FFmpegThread;
//import com.pfe.config.Kill;
import com.pfe.config.Kill;


 public class test {

 public static void main(String[] args) throws Exception {

    String ip="127.0.0.1";
    int port=12000;
    FFmpeg fFmpeg = new FFmpeg();
    List <String> ds = fFmpeg.listDiveces();

    if(!ds.isEmpty()){
        Thread thread = new Thread(new FFmpegThread(ip, port, ds.get(0), ds.get(1)));
        thread.start();
    }

    Thread thread = new Thread(new FFPlayThread(ip, port));
    thread.start();
    /*int i=0;
    while(i<10000){
        i++;
        System.out.println(i);
    }
    if(Kill.isProcessRunging("ffmpeg.exe"))
        Kill.killProcess("ffmpeg.exe");
    if(Kill.isProcessRunging("ffplay.exe"))
        Kill.killProcess("ffplay.exe");*/


}}

答案 1 :(得分:0)

您应该尝试刷新/关闭输出流。它应该工作:

public void SplitVideo(String lokasi) {
try {
    //excecute cmd
    String cmd = "c:\\ff\\bin\\ffmpeg.exe -i " + lokasi + " -y -f image2 c:\\vid\\img-%07d.jpg";
    Process jalan = Runtime.getRuntime().exec(cmd);
    // Get output stream to write from it
    OutputStream keluaran = jalan.getOutputStream();
    System.out.println(keluaran);
    keluaran.flush();
   keluaran.close(); 
} catch (IOException e) {
    System.out.println(e);
}

}

Flush基本上刷新此输出流并强制写出任何缓冲的输出字节。在这里阅读更多内容:

http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#flush()