本来我是打算通过ffmpeg进行图片和成视频的,但是不知道什么原因编码出来后,都会只剩三秒,后来发现,可以直接通过ffmpeg进行录屏。
首先就是先下载ffmpeg。这里我将dll文件和exe文件放到了同一个目录下,避免一些麻烦。
然后就是录屏命令了,很多博客上都需要各种插件才可以,但我发现了一个命令,可以直接调用:
ffmpeg -f gdigrab -i desktop out.mpg
这样就可以直接在当前目录下输出out.mpg的录像文件,当然也可以指定其他格式
如何进行推流呢?
ffmpeg -f gdigrab -i desktop -f flv rtmp://127.0.0.1:8082/接口
就将流推送到rtmp服务器中了,本人在自己的电脑中搭建了一个nginx作为推流地址。
如果在java中调用的话,我本人是通过生成cmd命令进行调用的。
粘一段小代码,是我的一个测试类
package top.whbweb.screen.testCMD;
import org.junit.Test;
import top.whbweb.screen.recording.util.UUID;
import java.io.*;
import java.net.URL;
public class TestCMD {
public static void main(String[] args) throws IOException {
TestCMD testCMD = new TestCMD();
StringBuilder cmd=new StringBuilder();
cmd.append(testCMD.getffmpegPath()+"ffplay -f gdigrab -i desktop ");//.append("-f flv ");
testCMD.exce(cmd.toString());
}
public void imgToVideo() throws IOException {
TestCMD testCMD = new TestCMD();
testCMD.play();
String path = testCMD.getffmpegPath();
/*-f image2 */
String cmd = path + "ffmpeg -loop 1 -f image2 -i D:/20180115153033300333833460416205/%04d.jpg "
+ "-vcodec libx264 -r 5 -t 30 D:/capture/" + UUID.getUUID() + ".mp4";
testCMD.exce(cmd.toString());
}
public void play() throws IOException {
TestCMD testCMD = new TestCMD();
String path = testCMD.getffmpegPath();
StringBuilder playVideo=new StringBuilder();
playVideo.append(path).append("ffplay.exe ");
playVideo.append("D:\\date\\file\\testData\\26.mp4");
testCMD.exce("calc");
}
public void exce(final String cmd) throws IOException {
Runtime.getRuntime().exec("cmd /c " + cmd);
}
private String getffmpegPath() {
String path = TestCMD.class.getClassLoader().getResource("top/whbweb/screen/").getPath().substring(1);
return path + "ffmpeg/windows/";
}
}