国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

使用Java和ffmpeg把音頻和視頻合成視頻的操作方法

瀏覽:149日期:2022-09-05 08:50:39

FFmpeg是一個開源免費跨平臺的視頻和音頻流方案,屬于自由軟件,采用LGPL或GPL許可證(依據(jù)你選擇的組件)。它提供了錄制、轉(zhuǎn)換以及流化音視頻的完整解決方案。它包含了非常先進的音頻/視頻編解碼庫libavcodec,為了保證高可移植性和編解碼質(zhì)量,libavcodec里很多codec都是從頭開發(fā)的。

FFmpeg是一套可以用來記錄、轉(zhuǎn)換數(shù)字音頻、視頻,并能將其轉(zhuǎn)化為流的開源計算機程序。它包括了目前領(lǐng)先的音/視頻編碼庫libavcodec。 FFmpeg是在Linux下開發(fā)出來的,但它可以在包括Windows在內(nèi)的大多數(shù)操作系統(tǒng)中編譯。這個項目是由Fabrice Bellard發(fā)起的,現(xiàn)在由Michael Niedermayer主持。可以輕易地實現(xiàn)多種視頻格式之間的相互轉(zhuǎn)換,例如可以將攝錄下的視頻avi等轉(zhuǎn)成現(xiàn)在視頻網(wǎng)站所采用的flv格式

主要功能:

1、視頻格式轉(zhuǎn)換功能

ffmpeg視頻轉(zhuǎn)換功能。視頻格式轉(zhuǎn)換,比如可以將多種視頻格式轉(zhuǎn)換為flv格式,可不是視頻信號轉(zhuǎn)換 。

ffmpeg可以輕易地實現(xiàn)多種視頻格式之間的相互轉(zhuǎn)換(wma,rm,avi,mod等),例如可以將攝錄下的視頻avi等轉(zhuǎn)成現(xiàn)在視頻網(wǎng)站所采用的flv格式。

2、視頻截圖功能

對于選定的視頻,截取指定時間的縮略圖。視頻抓圖,獲取靜態(tài)圖和動態(tài)圖,不提倡抓gif文件;因為抓出的gif文件大而播放不流暢

3、給視頻加水印功能

使用ffmpeg 視頻添加水印(logo)。

好了,下面開始今天的正文。

借助第三方工具ffmpeg合成視頻

需求:在小破站上下載了一些視頻,但是放到電腦里面看,我擦,聲音文件和視頻文件是分開的。

正確安裝ffmpeg并配置好環(huán)境變量。 Java代碼測試

使用Java和ffmpeg把音頻和視頻合成視頻的操作方法

里面是下載的視頻和音頻

使用Java和ffmpeg把音頻和視頻合成視頻的操作方法

我就上代碼遞歸了,只要用正確的ffmpeg的命令和Java調(diào)用ffmpeg.exe的程序,就可以合成啦。

package com.lovely.test;import java.io.BufferedReader;import java.io.File;//import java.io.FileInputStream;//import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import java.util.Date;import java.util.UUID;/** * * 視頻中獲取音頻文件 * */public class TestFfmpeg { // FFmpeg全路徑 private static final String FFMPEG_PATH = 'D:softWaretoolsjoyToolffmpegbinffmpeg.exe'; public static void main(String[] args) { String path = 'E:StudyVedioComputerScienceUS'; try { getAll(path); } catch (Exception e) { e.printStackTrace(); } } /** * 具體合成視頻函數(shù) * @param videoInputPath * 原視頻的全路徑 * * @param audioInputPath * 音頻的全路徑 * * @param videoOutPath * 視頻與音頻結(jié)合之后的視頻的路徑 */ public static void convetor(String videoInputPath, String audioInputPath, String videoOutPath) throws Exception { Process process = null; InputStream errorStream = null; InputStreamReader inputStreamReader = null; BufferedReader br = null; try { // ffmpeg命令 String command = FFMPEG_PATH + ' -i ' + videoInputPath + ' -i ' + audioInputPath + ' -c:v copy -c:a aac -strict experimental ' + ' -map 0:v:0 -map 1:a:0 ' + ' -y ' + videoOutPath; process = Runtime.getRuntime().exec(command); errorStream = process.getErrorStream(); inputStreamReader = new InputStreamReader(errorStream); br = new BufferedReader(inputStreamReader); // 用來收集錯誤信息的 String str = ''; while ((str = br.readLine()) != null) { System.out.println(str); } process.waitFor(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { br.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (errorStream != null) { errorStream.close(); } } } // 遞歸函數(shù) public static void getAll(String path) throws Exception { String videoInputPath = ''; String audioInputPath = ''; String videoOutPath = ''; File file = new File(path); if (file.isDirectory()) { File[] files = file.listFiles(); for (File f : files) { getAll(f.getPath()); if (f.isFile()) { if (f.getName().endsWith('.m4s')) { if (f.getName().endsWith('audio.m4s')) audioInputPath = file.getPath() + 'audio.m4s'; if (f.getName().endsWith('video.m4s')) videoInputPath = file.getPath() + 'video.m4s'; videoOutPath = file.getPath() + 'all.mp4'; if (!videoInputPath.equals('')) convetor(videoInputPath, audioInputPath, videoOutPath); } } } } }}

我最后用了好幾分鐘合成了30個完整的視頻。體會了遞歸的強大。

總結(jié)

到此這篇關(guān)于使用Java和ffmpeg把音頻和視頻合成視頻的操作方法的文章就介紹到這了,更多相關(guān)java ffmpeg音頻合成視頻內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 沭阳县| 邵东县| 綦江县| 郴州市| 海兴县| 泰来县| 岢岚县| 错那县| 安多县| 高碑店市| 曲麻莱县| 东乌| 永定县| 平阴县| 永德县| 定日县| 承德市| 临沧市| 怀集县| 大关县| 大冶市| 福清市| 习水县| 离岛区| 江源县| 西安市| 宁武县| 铜鼓县| 清涧县| 武冈市| 郯城县| 监利县| 遵化市| 福海县| 张家口市| 喀喇沁旗| 噶尔县| 卓资县| 佛坪县| 乌拉特后旗| 宜宾县|