VideoConvert_CutPic.java

import java.io.File;  
import java.util.ArrayList;  
import java.util.Calendar;  
import java.util.List;  
/**
 * 视频转码_截图
 */
public class VideoConvert_CutPic {
    private final static String srcFilePath = "D:/ffmpeg_mencoder_File/sourceVideos/短视频.mp4";  //源视频文件路径
      
    public static void main(String[] args) {  
        if (!is_File(srcFilePath)) {   //判断路径是不是一个文件
            System.out.println(srcFilePath + " is not file");  
            return;  
        }  
        if (executeCodecs()) {        //执行转码任务
            System.out.println("ok");  
        }  
    }  
    /**
     * 判断路径是不是一个文件
     *
     * @param file
     *            源视频文件路径
     */
    private static boolean is_File(String path) {  
        File file = new File(path);  
        if (!file.isFile()) {  
            return false;  
        }  
        return true;  
    }
    
    /**
     * 判断视频格式
     */
    private static int is_VideoType() {  
        String type = srcFilePath.substring(srcFilePath.lastIndexOf(".") + 1, srcFilePath.length()).toLowerCase();  
        // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)  
        if (type.equals("avi")) {  
            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("mov")) {  
            return 0;  
        } else if (type.equals("mp4")) {  
            return 0;  
        } else if (type.equals("asf")) {  
            return 0;  
        } else if (type.equals("asx")) {  
            return 0;  
        } else if (type.equals("flv")) {  
            return 0;  
        }  
        // 对ffmpeg.exe无法解析的文件格式(wmv9,rm,rmvb等),可以先用别的工具(mencoder.exe)转换为.avi(ffmpeg能解析的格式).  
        else if (type.equals("wmv9")) {  
            return 1;  
        } else if (type.equals("rm")) {  
            return 1;  
        } else if (type.equals("rmvb")) {  
            return 1;  
        }  
        return 9;  
    }
    /**
     * 源视频转换成AVI格式
     *
     * @param type
     *            视频格式
     */
    // 对ffmpeg.exe无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder.exe)转换为avi(ffmpeg能解析的)格式.  
    private static String convertToAVI(int type) {  
        List<String> commend = new ArrayList<String>();  
        commend.add("D:\\ffmpeg_mencoder_File\\Tools\\mencoder.exe");  
        commend.add(srcFilePath);  
        commend.add("-oac");  
        commend.add("lavc");  
        commend.add("-lavcopts");  
        commend.add("acodec=mp3:abitrate=64");  
        commend.add("-ovc");  
        commend.add("xvid");  
        commend.add("-xvidencopts");  
        commend.add("bitrate=600");  
        commend.add("-of");  
        commend.add("avi");  
        commend.add("-o");  
        commend.add("D:\\ffmpeg_mencoder_File\\targetVideos\\oneRedioAfter.avi"); //【存放转码后视频的路径,记住一定是.avi后缀的文件名】
        try {  
            //调用线程命令启动转码
            ProcessBuilder builder = new ProcessBuilder();  
            builder.command(commend);  
            builder.start();  
            return "D:\\ffmpeg_mencoder_File\\targetVideos\\oneRedioAfter.avi";  //【存放转码后视频的路径,记住一定是.avi后缀的文件名】
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
 
    /**
     * 源视频转换成FLV格式
     * @param srcFilePathParam
     *            源:用于指定要转换格式的文件,要截图的源视频文件路径
     */
    // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)  
    private static boolean convertToFLV(String srcFilePathParam) {  
          if (!is_File(srcFilePath)) {  
            System.out.println(srcFilePathParam + " is not file");  
            return false;  
        }  
        // 文件命名  
        Calendar c = Calendar.getInstance();  
        String savename = String.valueOf(c.getTimeInMillis())+ Math.round(Math.random() * 100000);  
        List<String> commend = new ArrayList<String>();  
        commend.add("D:\\ffmpeg_mencoder_File\\Tools\\ffmpeg.exe");  
        commend.add("-i");  
        commend.add(srcFilePathParam);  
        commend.add("-ab");  
        commend.add("56");  
        commend.add("-ar");  
        commend.add("22050");  
        commend.add("-qscale");  
        commend.add("8");  
        commend.add("-r");  
        commend.add("15");  
        commend.add("-s");  
        commend.add("600x500");  
        commend.add("D:\\ffmpeg_mencoder_File\\targetVideos\\oneRedioAfter.flv");  //【存放转码后视频的路径,记住一定是.flv后缀的文件名】
        try {  
            Runtime runtime = Runtime.getRuntime();  
            Process proce = null;
            String cutPicPath = "     D:\\ffmpeg_mencoder_File\\Tools\\ffmpeg.exe   -i   "  
                    + srcFilePathParam  
                    + "   -y   -f   image2   -ss   2   -t   0.001   -s   600x500   d:\\ffmpeg_mencoder_File\\cutPicture\\"  
                    + "oneCutPic.jpg";  //截图文件的保存路径  
            proce = runtime.exec(cutPicPath);
            //调用线程命令进行转码
            ProcessBuilder builder = new ProcessBuilder(commend);                 
            builder.command(commend);  
            builder.start();  
            return true;  
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }  
    }
    /**
     * 视频转码(mencoder.exe或ffmpeg.exe执行编码解码)
     */
    private static boolean executeCodecs() {
        // 判断视频的类型
        int type = is_VideoType();  
        boolean status = false;  
        //如果是ffmpeg可以转换的类型直接转码,否则先用mencoder转码成AVI
        if (type == 0) {  
            System.out.println("直接将文件转为flv文件");  
            status = convertToFLV(srcFilePath);// 直接将文件转为flv文件  
        } else if (type == 1) {  //
            String codcFilePath = convertToAVI(type); //视频格式转换后的目标视频文件路径
            if (codcFilePath == null){
                return false;// avi文件没有得到  
            }    
            status = convertToFLV(codcFilePath);// 将avi转为flv  
        }  
        return status;  
    }
}
/**
 * 参考
 * java-ffmpeg_mencoder(一) 实现视频的转码和截图功能https://www.cnblogs.com/tohxyblog/p/6640786.html
 * 命令格式:
普通转码:
ffmpeg -i [输入文件名] [参数选项] -f [格式] [输出文件]
比如:MP4转avi    D:\\ffmpeg_mencoder_File\\Tools\\ffmpeg.exe -i
                    D:/ffmpeg_mencoder_File/sourceVideos/短视频.mp4 -f avi
                    D:\\ffmpeg_mencoder_File\\targetVideos\\oneRedioAfter.avi
参数:-an 去掉音频
-b 1000K 压缩码率为1000K
-s 800*500 压缩尺寸为800*500
-aspect 宽高比,格式 16:9 或 1.778
(当源尺寸是16:9,要压缩成非16:9的,一方面要设定 -s,另一方面要给出 -aspect)
-ss 开始时间​
-t 持续时长
 */

VideoPicTest.java

import ljx.dao.VideoPicDao;
import ljx.service.VideoPicService;public class VideoPicServiceImpl implements VideoPicService{private VideoPicDao videoPicDao;public VideoPicDao getVideoPicDao() {return videoPicDao;}public void setVideoPicDao(VideoPicDao videoPicDao) {this.videoPicDao = videoPicDao;}public boolean executeCodecs(String srcFilePath, String codcFilePath,String mediaPicPath) {return videoPicDao.executeCodecs(srcFilePath, codcFilePath, mediaPicPath);}
}

VideoPicService.java

public interface VideoPicService {/*** 视频转码* @param srcFilePath  用于指定要转换格式的文件,要截图的视频源文件* @param codcFilePath 格式转换后的的文件保存路径* @param mediaPicPath 截图保存路径* @return*/public boolean executeCodecs(String srcFilePath, String codcFilePath, String mediaPicPath);
}

VideoPicServiceImpl.java

import ljx.dao.VideoPicDao;
import ljx.service.VideoPicService;public class VideoPicServiceImpl implements VideoPicService{private VideoPicDao videoPicDao;public VideoPicDao getVideoPicDao() {return videoPicDao;}public void setVideoPicDao(VideoPicDao videoPicDao) {this.videoPicDao = videoPicDao;}public boolean executeCodecs(String srcFilePath, String codcFilePath,String mediaPicPath) {return videoPicDao.executeCodecs(srcFilePath, codcFilePath, mediaPicPath);}
}

VideoPicDao.java

public interface VideoPicDao {/*** 视频转码(mencoder.exe或ffmpeg.exe执行编码解码)* * @param srcFilePath*            源:用于指定要转换格式的文件,要截图的源视频文件路径* @param codcFilePath*            目标:视频格式转换后的的目标视频文件保存路径* @param mediaPicPath*            截图保存路径* @return*/public boolean executeCodecs(String srcFilePath, String codcFilePath, String mediaPicPath);
}

VideoPicDaoImpl.java

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;import ljx.dao.VideoPicDao;public class VideoPicDaoImpl implements VideoPicDao{/*** 判断源视频文件格式是ffmpeg.exe能解析的格式* * @param file*            源视频文件路径*/public boolean is_ffmpegVideoType(String file) {boolean result = false;String ext = file.substring(file.lastIndexOf(".") + 1, file.length()).toLowerCase();// ffmpeg.exe能解析并相互转换的的源文件格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)// 对ffmpeg.exe无法解析的源文件格式(wmv9,rm,rmvb等),可以先用别的工具(mencoder.exe)解析;然后转换为.avi(ffmpeg.exe能解析的格式).if (ext.equals("avi")) {result = true;} else if (ext.equals("mpg")) {result = true;} else if (ext.equals("wmv")) {result = true;} else if (ext.equals("3gp")) {result = true;} else if (ext.equals("mov")) {result = true;} else if (ext.equals("mp4")) {result = true;} else if (ext.equals("asf")) {result = true;} else if (ext.equals("asx")) {result = true;} else if (ext.equals("flv")) {result = true;}return result;}/*** 判断源视频文件格式是mencoder.exe能解析的格式* * @param file*            源视频文件路径*/public boolean is_mencoderVideoType(String file) {boolean result = false;String ext = file.substring(file.lastIndexOf(".") + 1, file.length()).toLowerCase();if (ext.equals("wmv9")) {result = true;} else if (ext.equals("rm")) {result = true;} else if (ext.equals("rmvb")) {result = true;}return result;}/*** 源视频转换成FLV格式* * @param ffmpegPath*            ffmpeg.exe转换工具路径* @param srcFilePath*            源:用于指定要转换格式的文件,要截图的源视频文件路径* @param codcFilePath*            目标:视频格式转换后的的目标视频文件保存路径*/private boolean convertToFLV(String ffmpegPath, String srcFilePath,String codcFilePath) {File file = new File(ffmpegPath);File srcFile = new File(srcFilePath);if (file.exists()) {System.out.println("转换工具存在");}if (srcFile.exists()) {System.out.println("源视频存在");}// 创建一个List集合来保存转换视频文件为flv格式的命令List<String> convert = new ArrayList<String>();convert.add(ffmpegPath); // 添加转换工具路径convert.add("-i"); // 添加参数"-i",该参数指定要转换的文件convert.add(srcFilePath); // 添加要转换格式的视频文件的路径convert.add("-ab"); // 设置音频码率convert.add("128");convert.add("-ac"); // 设置声道数convert.add("2");convert.add("-qscale");convert.add("6");convert.add("-ar"); // 设置声音的采样频率convert.add("22050");convert.add("-r"); // 设置帧频convert.add("29.97");convert.add("-b");convert.add("5942.13");convert.add("-s");convert.add("1280x720");convert.add("-y"); // 添加参数"-y",该参数指定将覆盖已存在的文件convert.add(codcFilePath);boolean mark = true;try {Process proc = new ProcessBuilder(convert).redirectErrorStream(true).start();BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));String line;while ((line = stdout.readLine()) != null) {System.out.println(line);}} catch (Exception e) {mark = false;System.out.println(e);e.printStackTrace();}return mark;}/*** 源视频转换成AVI格式* * @param mencoderPath*            mencoder.exe转换工具路径* @param srcFilePath*            源:用于指定要转换格式的文件,要截图的源视频文件路径* @param codcFilePath*            目标:视频格式转换后的的目标视频文件保存路径*/private boolean convertToAVI(String mencoderPath, String srcFilePath,String codcFilePath) {List<String> commend = new ArrayList<String>();commend.add(mencoderPath);commend.add(srcFilePath);commend.add("-oac");commend.add("lavc");commend.add("-lavcopts");commend.add("acodec=mp3:abitrate=64");commend.add("-ovc");commend.add("xvid");commend.add("-xvidencopts");commend.add("bitrate=600");commend.add("-of");commend.add("avi");commend.add("-o");commend.add(codcFilePath);try {ProcessBuilder builder = new ProcessBuilder();builder.command(commend);builder.redirectErrorStream(true);// 后续子进程错误输出与标准输出合并Process p = builder.start();p.getInputStream();// 后续进程等待Mencoder进程转换结束后才可进行p.waitFor();return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 从指定的视频文件(即可以是源视频文件,也可以是转换后的目标视频文件)中截图* * @param ffmpegPath*            ffmpeg.exe转换工具路径* @param srcFilePath*            源:用于指定要转换格式的文件,要截图的源视频文件路径* @param mediaPicPath*            截图保存路径*/private Boolean cutPic(String ffmpegPath, String srcFilePath,String mediaPicPath) {// 创建一个List集合来保存从视频中截取图片的命令List<String> cutpic = new ArrayList<String>();cutpic.add(ffmpegPath);cutpic.add("-i");cutpic.add(srcFilePath); // 同上(指定的文件即可以是转换为flv格式之前的文件,也可以是转换的flv文件)cutpic.add("-y");cutpic.add("-f");cutpic.add("image2");cutpic.add("-ss"); // 添加参数"-ss",该参数指定截取的起始时间cutpic.add("1"); // 添加起始时间为第11秒cutpic.add("-t"); // 添加参数"-t",该参数指定持续时间cutpic.add("0.001"); // 添加持续时间为1毫秒cutpic.add("-s"); // 添加参数"-s",该参数指定截取的图片大小cutpic.add("800*280"); // 添加截取的图片大小为350*240cutpic.add(mediaPicPath); // 添加截取的图片的保存路径boolean mark = true;ProcessBuilder builder = new ProcessBuilder();try {builder.command(cutpic);builder.redirectErrorStream(true);// 如果此属性为 true,则任何由通过此对象的 start() 方法启动的后续子进程生成的错误输出都将与标准输出合并,// 因此两者均可使用 Process.getInputStream() 方法读取。这使得关联错误消息和相应的输出变得更容易builder.start();} catch (Exception e) {mark = false;System.out.println(e);e.printStackTrace();}return mark;}/*** 删除转换后的目标视频文件* @param tempFile  转换后的目标视频文件*/public void deleteAVIFile(String tempFile) {File file = new File(tempFile);if (file.exists()) {file.delete();}}/*** 视频转码(mencoder.exe或ffmpeg.exe执行编码解码)* * @param srcFilePath*            源:用于指定要转换格式的文件,要截图的源视频文件路径* @param codcFilePath*            目标:视频格式转换后的的目标视频文件保存路径* @param mediaPicPath*            截图保存路径* @return*/public boolean executeCodecs(String srcFilePath, String codcFilePath,String mediaPicPath) {String basePath = System.getProperty("user.dir");// 当前工程路径        String mencoderPath = "D:\\ffmpeg_mencoder_File\\Tools\\mencoder.exe";   String ffmpegPath = "D:\\ffmpeg_mencoder_File\\Tools\\ffmpeg.exe";// 转换工具路径boolean mark = true;String tempPath = basePath + File.separator + "temp" + File.separator + String.valueOf(System.currentTimeMillis()) + ".avi";       if (is_mencoderVideoType(srcFilePath)) {//判断是否可转换为AVI视频文件// mencoder.exe能解析并转换的源文件格式:(wmv9,rm,rmvb等)mark = this.convertToAVI(mencoderPath, srcFilePath, tempPath); srcFilePath = tempPath;}if (is_ffmpegVideoType(srcFilePath) && mark) {//判断是否可转换为FLV视频文件// ffmpeg.exe能解析并转换的源文件格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)mark = this.convertToFLV(ffmpegPath, srcFilePath, codcFilePath);mark = this.cutPic(ffmpegPath, srcFilePath, mediaPicPath);} else {System.out.println("该视频格式无法转换");mark = false;}this.deleteAVIFile(tempPath);return mark;}
}

CutPic.jsp

<body>
    <img src="http://localhost:8081/virtualCutPicUrl/oneCutPic.jpg" alt="图片预览2" > <br><!--虚拟路径-->
  </body>

<!-- 备注
1.tomcat 下server.xml文件中需要配置

<HOST>

<!--增加的下面这行中path="/虚拟名" docBase="虚拟路径,为本地物理路径" -->
    <Context path="/virtualCutPicUrl" docBase="D:\ffmpeg_mencoder_File\cutPicture\" reloadable="true"></Context>

</HOST>

2.CutPic.jsp文件中,格式为
<img src='http://ip+端口/虚拟路径/文件夹/文件名'>  
-->

<!--参考
 网站实现视频上传、转码、截图及在线播放功能https://blog.csdn.net/autumn20080101/article/details/51171442

-->

百度云链接链接:https://pan.baidu.com/s/1VB7IBX1MqHOqY991udMO-A 密码:5k7g

视频截图 使用ffmpeg_mencoder相关推荐

  1. C#Winform+WindowsAPI做个剪贴板无缝自动保存器(视频截图利器)

    C#Winform+WindowsAPI做个剪贴板无缝自动保存器(视频截图利器) (本文最新代码已上传到GitHub,地址在(https://github.com/bitzhuwei/Clipboar ...

  2. js截屏 video_canvas与html5实现视频截图功能

    这段时间一直在研究canvas,突发奇想想做一个可以截屏视频的功能,然后把图片拉去做表情包,哈哈哈哈哈哈~~ 制作方法: 1.在页面中加载视频 在使用canvas制作这个截图功能时,首先必须保证页面上 ...

  3. ios 获取视频截图

    #pragma mark -- 获取视频截图 - (UIImage *)getThumbnailImage:(NSString *)videoURL{     AVURLAsset *asset = ...

  4. js截屏 video_用原生JS和html5进行视频截图并保存到本地

    Video视频截图 body, h1, h2, p { margin:0; padding:0; } html { font-family:"微软雅黑"; background-c ...

  5. 阿里云Aliplayer高级功能介绍(一):视频截图

    基本介绍 H5 Video是不提供截图的API的, 视频截图需要借助Canvas,通过Canvas提供的drawImage方法,把Video的当前画面渲染到画布上, 最终通过toDataURL方法可以 ...

  6. ASP.NET 视频截图功能的C#代码

    前公司在制作播客系统(Web程序)中,用到从视频截图功能.下边是截图CatchImg方法,可从大多数的视频文件中截图成功,大家可测试;如果截图不成功,大多是因为视频本身的问题,如编码标准或加了密.但从 ...

  7. [html] H5播放的video视频,如何实现对视频截图?

    [html] H5播放的video视频,如何实现对视频截图? <video controls src="./assets/demo.mp4" width="400& ...

  8. ffmpeg 截图 java_Java Web 中使用ffmpeg实现视频转码、视频截图

    视频网站中提供的在线视频播放功能,播放的都是FLV格式的文件,它是Flash动画文件,可通过Flash制作的播放器来播放该文件.项目中用制作的player.swf播放器. 多媒体视频处理工具FFmpe ...

  9. ffmpeg获取视频截图

    需求 使用ffmpeg可以非常方便的生成视频截图,ffmpeg 通过指定 -vcodec 参数为 mjpeg,或者指定 -f 参数为 mjpeg时,可以输出 jpg截图,指定 -vcodec参数为pn ...

  10. 用matlab实现视频截图字幕部分的拼接

    这里提供一个在线截图拼接工具:截图拼接工具 - 在线拼接电影字幕截图 我们常在微博及各大论坛.贴吧看到各种视频截图字幕部分的拼接,这广泛应用于一个角色在剧中说的一段特别精彩的话,这对应于 Gif ⇒ ...

最新文章

  1. 服务器缓存策略(304)
  2. JZOJ 100045. 【NOIP2017提高A组模拟7.13】好数
  3. linux 内核 ntfs,Linux大脑 内核 内核编译(NTFS)
  4. python中if elif else流程图_Python中的if、else、elif语句用法简明讲解
  5. cts测试终于全测了一遍了
  6. 高级版本 【多后台】
  7. 史上最简单的 SpringCloud 教程
  8. Python命令行模式下调试程序
  9. jQuery:自学笔记(3)——操作DOM
  10. SQL 基础教程 练习题 Chapter 1
  11. 如何提高信号发生器(信号源)测量时的幅度精度
  12. 2020年精选网络性能监控系统
  13. 【前端面试题(3)】
  14. excel导入时手机号码格式错误的一个解决方法
  15. 教你制作第一个C++游戏!#1 引入
  16. EVA-中文开放域对话预训练模型
  17. [SEO工具]新站优化推广工具集
  18. Ueditor 自定义多图上传路径及回显
  19. 世界杯高清直播背后的五大科技护法
  20. 几何画板 5.07 最强中文版

热门文章

  1. 计算机三级网络技术备考复习资料
  2. 大漠找图算法_新手用大漠找图识别数字,怎么将数字组合起来呢
  3. Virtual Display Manager(windows虚拟显示器软件)官方中文版V3.3.2.44650 | Win7/win10虚拟显示器下载
  4. 福利来了,axure8.1注册码
  5. python航空订票系统_航空订票系统
  6. 初中毕业能学习软件测试吗,我只是初中毕业而且23岁了会不会太晚我想学 – 手机爱问...
  7. 乐优商城(01)--项目启动
  8. GOF设计模式--简单工厂模式
  9. Matlab中的画图函数
  10. 以太坊使用及代币开发实战