视频截图 使用ffmpeg_mencoder
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重装Intel核显后,设备管理器英特尔显
- 下一篇: 工信部数字电视标准符合性检测中心发布的