龙空技术网

ffmpeg的使用,通过Java来进行对视频的转码和截图

那一丝涟漪 1013

前言:

此时各位老铁们对“java截取视频”大约比较注意,小伙伴们都需要知道一些“java截取视频”的相关资讯。那么小编同时在网上网罗了一些有关“java截取视频””的相关知识,希望小伙伴们能喜欢,同学们一起来了解一下吧!

1.配置ffmpeg的环境变量2.代码如下

import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;/** * @Author LinXin * 1.设置视频输出目录 * 2.获取上传视频的路径以及文件名 * 3.截取后缀名判断格式 * 4.ffmpeg不支持的格式先使用mencoder转成AVI格式  丢弃 * 5.AVI再转成flv格式,同时删除缓存文件           丢弃 */public class Video {    //视频输出目录    private static final String OutSrc = "C:/Users/LinXin/Desktop/测试视频/flv/";    //截图输出目录    private static final String imageOutSrc = "C:/Users/LinXin/Desktop/测试视频/Screen/";    //视频类型    private static final String OutType = ".flv";    //图片类型    private static final String ImagesType = ".jpg";    //转码完视频路径    private static String videoOutSrc = null;    public static void main(String[] args) {        String fileName = "C:/Users/LinXin/Desktop/测试视频/video/1.mp4";        String type = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();        File orginFile = new File(fileName);        long start = System.currentTimeMillis();        if (!orginFile.exists()) {            System.out.println("文件不存在");        } else {            int i = checkType(type);            if (i == 0) {                String OutName = fileName.substring(fileName.lastIndexOf("/") +1, fileName.lastIndexOf(".")) + OutType;                File file = new File(OutSrc + OutName);                if (file.exists()) {                    System.out.println("文件已存在");                } else {                    String command = "cmd /c ffmpeg -i " + fileName + " -c:v copy -c:a copy " + OutSrc + OutName;                    if (procedure(command)) {                        File file1 = new File(fileName);//                        file1.delete();                        System.out.println("Success");                        videoOutSrc = OutSrc+OutName;                        String fileNameWithNoneType = fileName.substring(fileName.lastIndexOf("/") +1, fileName.lastIndexOf("."));                        ScreenShot(videoOutSrc,fileNameWithNoneType);                    } else {                        System.out.println("Failure");                    }                }            } else {                System.out.println("文件类型不支持");            }        }        long end = System.currentTimeMillis();        System.out.println("");        System.out.println("耗时:"+(end-start)/1000);    }    //视频转码,图片截取    public static boolean procedure(String command) {        boolean flag = false;        try {            Runtime rt = Runtime.getRuntime();            Process p = rt.exec(command);            //一个线程去获取正常信息            new Thread(new Runnable() {                BufferedReader infoReader = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));                String line1 = null;                @Override                public void run() {                    try {                        while ((line1 = infoReader.readLine()) != null) {                            System.out.println(line1);                        }                    } catch (IOException e) {                        e.printStackTrace();                    }                }            }).start();            //一个线程去获取错误信息            new Thread(new Runnable() {                BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream(), "GBK"));                String line2 = null;                @Override                public void run() {                    try {                        while ((line2 = errorReader.readLine()) != null) {                            System.out.println(line2);                        }                    } catch (IOException e) {                        e.printStackTrace();                    }                }            }).start();            int exitValue = p.waitFor();            if (exitValue == 0) {                flag = true;            }            p.destroy();            System.out.println("Exited with error code " + exitValue);        } catch (Exception e) {            System.out.println(e.getMessage());            flag = false;        }        return flag;    }    //ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv,mkv等)    private static int checkType(String type) {        if (type.equals("asx")) {            return 0;        } else if (type.equals("asf")) {            return 0;        } else if (type.equals("mpg")) {            return 0;        } else if (type.equals("wmv")) {            return 0;        } else if (type.equals("3gp")) {            return 0;        } else if (type.equals("mp4")) {            return 0;        } else if (type.equals("mov")) {            return 0;        } else if (type.equals("avi")) {            return 0;        } else if (type.equals("flv")) {            return 0;        } else if (type.equals("mkv")) {            return 0;        }        return 1;    }    //视频截图    public static boolean ScreenShot(String videoPath,String imagesPath){        boolean flag = false;        String command = "ffmpeg -ss 00:00:40 -t 0.001 -i "+videoPath+" -y -f image2 -q:v 2 -s 1920x1080 "+imageOutSrc+imagesPath+ImagesType;        if(procedure(command)){            System.out.println("ScreentShot Success");        }        return flag;    }}

标签: #java截取视频