本篇內(nèi)容主要講解“怎么使用ffmpeg實(shí)現(xiàn)視頻轉(zhuǎn)視頻”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么使用ffmpeg實(shí)現(xiàn)視頻轉(zhuǎn)視頻”吧!
創(chuàng)新互聯(lián)公司是專業(yè)的侯馬網(wǎng)站建設(shè)公司,侯馬接單;提供成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行侯馬網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
主要用到了FFmpeg這個(gè)工具,利用命令對(duì)視頻文件進(jìn)行操作。首先根據(jù)自己調(diào)的參數(shù)進(jìn)行圖片的截?。ū疚牡氖?秒10幀的參數(shù)),圖片轉(zhuǎn)換,然后分離音頻,最后字符圖片和音頻合成目標(biāo)視頻。
FFmpeg的代碼庫:
https://github.com/FFmpeg/FFmpeg
FFmpeg下載地址:
https://ffmpeg.org/download.html
本位使用的版本:
https://blog-lossingdawn.oss-cn-shanghai.aliyuncs.com/img2text/ffmpeg-20180201-b1af0e2-win64-static.zip
沒做細(xì)致的調(diào)整,視頻的轉(zhuǎn)換可以調(diào)調(diào)參數(shù)
@Test public static void videoTest() { String srcVideoPath = "F:/123/123.mp4"; String tarImagePath = "F:/123/mp/"; String tarAudioPath = "F:/123/mp/audio.aac"; String tarVideoPath = "F:/123/1234.mp4"; VideoUtil.readVideo(srcVideoPath,tarImagePath,tarAudioPath,tarVideoPath); }
主要用到的幾個(gè)命令,其他按幀截圖命令參考文末鏈接4:
// 截圖 ffmpeg -ss 10 -i input.flv -y -f image2 -vframes 100 -s 352x240 b-%03d.jpg // 分離音頻 ffmpeg -i 3.mp4 -vn -y -acodec copy 3.aac // 合成視頻 ffmpeg -threads2 -y -r 10 -i /tmpdir/image%04d.jpg -i audio.mp3 -absf aac_adtstoasc output.mp4
JDK 1.8
// ffmpeg -ss 10 -i input.flv -y -f image2 -vframes 100 -s 352x240 b-%03d.jpg /** * ffmpeg 截圖,并指定圖片的大小 * * @param srcVideoPath * @param tarImagePath * 截取后圖片路徑 * @param width * 截圖的寬 * @param hight * 截圖的高 * @param offsetValue * 表示相對(duì)于文件開始處的時(shí)間偏移值 可以是分秒 * @param vframes * 表示截圖的楨數(shù) * * @return */ public static boolean processFfmpegImage(String srcVideoPath, String tarImagePath, int width, int hight, float offsetValue, float vframes) { if (!checkfile(srcVideoPath)) { System.out.println("【" + srcVideoPath + "】 不存在 !"); // logger.error("【" + srcVideoPath + "】 不存在 !"); return false; } Listcommend = new java.util.ArrayList (); commend.add(ffmpegPath); commend.add("-i"); commend.add(srcVideoPath); commend.add("-y"); commend.add("-f"); commend.add("image2"); commend.add("-ss"); commend.add(offsetValue + ""); // 在視頻的某個(gè)插入時(shí)間截圖,例子為5秒后 // commend.add("-vframes"); commend.add("-t");// 添加參數(shù)"-t",該參數(shù)指定持續(xù)時(shí)間 commend.add(vframes + ""); // 截圖的楨數(shù),添加持續(xù)時(shí)間為1毫秒 commend.add("-s"); commend.add(width + "x" + hight); // 截圖的的大小 commend.add(tarImagePath); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commend); builder.redirectErrorStream(true); // builder.redirectOutput(new File("F:/123/log/log.log")); Process process = builder.start(); doWaitFor(process); process.destroy(); if (!checkfile(tarImagePath)) { System.out.println(tarImagePath + " is not exit! processFfmpegImage 轉(zhuǎn)換不成功 !"); // logger.info(tarImagePath + " is not exit! processFfmpegImage // 轉(zhuǎn)換不成功 !"); return false; } return true; } catch (Exception e) { e.printStackTrace(); System.out.println("【" + srcVideoPath + "】 processFfmpegImage 轉(zhuǎn)換不成功 !"); // logger.error("【" + srcVideoPath + "】 processFfmpegImage 轉(zhuǎn)換不成功 // !"); return false; } } public static boolean processFfmpegAudio(String srcVideoPath, String tarAudioPath) { if (!checkfile(srcVideoPath)) { System.out.println("【" + srcVideoPath + "】 不存在 !"); // logger.error("【" + srcVideoPath + "】 不存在 !"); return false; } // https://blog.csdn.net/xiaocao9903/article/details/53420519 // ffmpeg -i 3.mp4 -vn -y -acodec copy 3.aac // ffmpeg -i 3.mp4 -vn -y -acodec copy 3.m4a List commend = new java.util.ArrayList (); commend.add(ffmpegPath); commend.add("-i"); commend.add(srcVideoPath); commend.add("-vn"); commend.add("-y"); commend.add("-acodec"); commend.add("copy"); // 在視頻的某個(gè)插入時(shí)間截圖,例子為5秒后 commend.add(tarAudioPath); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commend); builder.redirectErrorStream(true); Process process = builder.start(); doWaitFor(process); process.destroy(); if (!checkfile(tarAudioPath)) { System.out.println(tarAudioPath + " is not exit! processFfmpegAudio 轉(zhuǎn)換不成功 !"); return false; } return true; } catch (Exception e) { e.printStackTrace(); System.out.println("【" + srcVideoPath + "】 processFfmpegAudio 轉(zhuǎn)換不成功 !"); return false; } } /** * ffmpeg 合成視頻 * * @param srcVideoPath * @param tarImagePath * 截取后圖片路徑 * @param width * 截圖的寬 * @param hight * 截圖的高 * @param offsetValue * 表示相對(duì)于文件開始處的時(shí)間偏移值 可以是分秒 * @param vframes * 表示截圖的楨數(shù) * * @return */ public static boolean processFfmpegVideo(String imagePath, String audioPath, String tarVideoPath, int step) { // https://blog.csdn.net/wangshuainan/article/details/77914508?fps=1&locationNum=4 // 帶音頻 // ffmpeg -threads2 -y -r 10 -i /tmpdir/image%04d.jpg -i audio.mp3 -absf // aac_adtstoasc output.mp4 List commend = new java.util.ArrayList (); commend.add(ffmpegPath); commend.add("-threads"); commend.add("2"); commend.add("-y"); commend.add("-r"); commend.add(step + ""); commend.add("-i"); commend.add(imagePath); // 圖片 commend.add("-i"); commend.add(audioPath); commend.add("-absf");// commend.add("aac_adtstoasc"); // commend.add(tarVideoPath); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commend); builder.redirectErrorStream(true); builder.redirectOutput(new File("F:/123/log/log.log")); Process process = builder.start(); doWaitFor(process); process.destroy(); if (!checkfile(tarVideoPath)) { System.out.println(tarVideoPath + " is not exit! processFfmpegVideo 轉(zhuǎn)換不成功 !"); return false; } return true; } catch (Exception e) { e.printStackTrace(); System.out.println("【" + tarVideoPath + "】 processFfmpegVideo 轉(zhuǎn)換不成功 !"); return false; } }
到此,相信大家對(duì)“怎么使用ffmpeg實(shí)現(xiàn)視頻轉(zhuǎn)視頻”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!