当前位置:网站首页 > Java基础 > 正文

java ffmpeg视频教程



FFmpeg是一个非常好用的视频处理工具,下面讲讲如何在java中使用该工具类。

一、首先,让我们来认识一下FFmpeg在Dos界面的常见操作

1.拷贝视频,并指定新的视频的名字以及格式

        ffmpeg.exe -i old.mp4 new.avi










2.将视频和音频结合,并指定视频的长度(7秒),同时生成结合之后的视频文件

        ffmpeg.exe -i tsd.mp4 -i “周笔畅+-+最美的期待.mp3” -t 7 -y new.avi




3.使用ffmpg生成视频截图(对第一秒的画面作为截图)-vframes表示帧数 

        ffmpeg.exe -ss 00:00:01 -y -i 视频.mp4 -vframes 1 new.jpg

 

二、在java中使用FFmpeg

   引入 FFMpegUtil工具类

 

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List;

/ * FFMPEG 的相关操作 * * @author Administrator */ public class FFMpegUtil {

<span style="color: rgba(128, 128, 128, 1)"><em>//Windows下 ffmpeg.exe的路径 

// private static String ffmpegEXE = “D:\Downloads\ffmpeg--ebf85d3-win64-static\bin\ffmpeg.exe”; //Linux与mac下 ffmpeg的路径 private static String ffmpegEXE = ”/developer/ffmpeg-4.0/bin/ffmpeg”;

<span style="color: rgba(128, 128, 128, 1)"><em>/ 

* @param videoInputPath 视频的输入路径 * @param videoOutPath 视频的输出路径 * @throws Exception */ // 拷贝视频,并指定新的视频的名字以及格式 // ffmpeg.exe -i old.mp4 new.avi public static void convetor(String videoInputPath, String videoOutPath) throws Exception {

 List&lt;String&gt; command = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>ArrayList&lt;String&gt;(); command.add(<span style="color: rgba(102, 14, 122, 1)"><em>ffmpegEXE</em></span>); command.add(<span style="color: rgba(0, 128, 0, 1)"><strong>"-i"</strong></span>); command.add(videoInputPath); command.add(videoOutPath); ProcessBuilder builder = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>ProcessBuilder(command); Process process = <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>; <span style="color: rgba(0, 0, 128, 1)"><strong>try </strong></span>{ process = builder.start(); } <span style="color: rgba(0, 0, 128, 1)"><strong>catch </strong></span>(IOException e) { <span style="color: rgba(128, 128, 128, 1)"><em>// </em></span><span style="color: rgba(0, 115, 191, 1)"><strong><em>TODO Auto-generated catch block 

e.printStackTrace();

 } <span style="color: rgba(128, 128, 128, 1)"><em>// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理 

InputStream errorStream = process.getErrorStream();

 InputStreamReader inputStreamReader = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>InputStreamReader(errorStream); BufferedReader br = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>BufferedReader(inputStreamReader); String line = <span style="color: rgba(0, 128, 0, 1)"><strong>""</strong></span>; <span style="color: rgba(0, 0, 128, 1)"><strong>while </strong></span>((line = br.readLine()) != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { } <span style="color: rgba(0, 0, 128, 1)"><strong>if </strong></span>(br != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { br.close(); } <span style="color: rgba(0, 0, 128, 1)"><strong>if </strong></span>(inputStreamReader != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { inputStreamReader.close(); } <span style="color: rgba(0, 0, 128, 1)"><strong>if </strong></span>(errorStream != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { errorStream.close(); } } <span style="color: rgba(128, 128, 128, 1)"><em>/ 

* @param videoInputPath 原视频的路径 * @param audioInputPath 音频的路径 * @param videoOutPath 视频与音频结合之后的视频的路径 * @param time 视频的长度 ,单位为 s * @throws Exception */ // 将视频和音频结合,并指定视频的长度,同时生成结合之后的视频文件 // ffmpeg.exe -i tsd.mp4 -i “周笔畅+-+最美的期待.mp3” -t 7 -y new.avi public static void convetor(String videoInputPath, String audioInputPath, String videoOutPath, double time)

 <span style="color: rgba(0, 0, 128, 1)"><strong>throws </strong></span>Exception { List&lt;String&gt; command = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>ArrayList&lt;String&gt;(); command.add(<span style="color: rgba(102, 14, 122, 1)"><em>ffmpegEXE</em></span>); command.add(<span style="color: rgba(0, 128, 0, 1)"><strong>"-i"</strong></span>); command.add(videoInputPath); command.add(<span style="color: rgba(0, 128, 0, 1)"><strong>"-i"</strong></span>); command.add(audioInputPath); command.add(<span style="color: rgba(0, 128, 0, 1)"><strong>"-t"</strong></span>); command.add(String.<span style="font-style: italic">valueOf</span>(time)); command.add(<span style="color: rgba(0, 128, 0, 1)"><strong>"-y"</strong></span>); command.add(videoOutPath); ProcessBuilder builder = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>ProcessBuilder(command); Process process = <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>; <span style="color: rgba(0, 0, 128, 1)"><strong>try </strong></span>{ process = builder.start(); } <span style="color: rgba(0, 0, 128, 1)"><strong>catch </strong></span>(IOException e) { <span style="color: rgba(128, 128, 128, 1)"><em>// </em></span><span style="color: rgba(0, 115, 191, 1)"><strong><em>TODO Auto-generated catch block 

e.printStackTrace();

 } <span style="color: rgba(128, 128, 128, 1)"><em>// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理 

InputStream errorStream = process.getErrorStream();

 InputStreamReader inputStreamReader = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>InputStreamReader(errorStream); BufferedReader br = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>BufferedReader(inputStreamReader); String line = <span style="color: rgba(0, 128, 0, 1)"><strong>""</strong></span>; <span style="color: rgba(0, 0, 128, 1)"><strong>while </strong></span>((line = br.readLine()) != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { } <span style="color: rgba(0, 0, 128, 1)"><strong>if </strong></span>(br != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { br.close(); } <span style="color: rgba(0, 0, 128, 1)"><strong>if </strong></span>(inputStreamReader != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { inputStreamReader.close(); } <span style="color: rgba(0, 0, 128, 1)"><strong>if </strong></span>(errorStream != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { errorStream.close(); } } <span style="color: rgba(128, 128, 128, 1)"><em>/ 

* @param time_coverimg 视频的第几秒作为封面图 * @param videoInputPath 视频的路径 * @param frame 帧数 * @param coverOutputPath 视频的封面图的路径 * @throws Exception */ // ffmpeg.exe -ss 00:00:01 -y -i 视频.mp4 -vframes 1 new.jpg


public static void convetor(String time_coverimg, String videoInputPath, int frame, String coverOutputPath)

 <span style="color: rgba(0, 0, 128, 1)"><strong>throws </strong></span>Exception { List&lt;String&gt; command = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>ArrayList&lt;String&gt;(); command.add(<span style="color: rgba(102, 14, 122, 1)"><em>ffmpegEXE</em></span>); command.add(<span style="color: rgba(0, 128, 0, 1)"><strong>"-ss"</strong></span>); command.add(time_coverimg); command.add(<span style="color: rgba(0, 128, 0, 1)"><strong>"-y"</strong></span>); command.add(<span style="color: rgba(0, 128, 0, 1)"><strong>"-i"</strong></span>); command.add(videoInputPath); command.add(<span style="color: rgba(0, 128, 0, 1)"><strong>"-vframes"</strong></span>); command.add(String.<span style="font-style: italic">valueOf</span>(frame)); command.add(coverOutputPath); ProcessBuilder builder = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>ProcessBuilder(command); Process process = <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>; <span style="color: rgba(0, 0, 128, 1)"><strong>try </strong></span>{ process = builder.start(); } <span style="color: rgba(0, 0, 128, 1)"><strong>catch </strong></span>(IOException e) { <span style="color: rgba(128, 128, 128, 1)"><em>// </em></span><span style="color: rgba(0, 115, 191, 1)"><strong><em>TODO Auto-generated catch block 

e.printStackTrace();

 } <span style="color: rgba(128, 128, 128, 1)"><em>// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理 

InputStream errorStream = process.getErrorStream();

 InputStreamReader inputStreamReader = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>InputStreamReader(errorStream); BufferedReader br = <span style="color: rgba(0, 0, 128, 1)"><strong>new </strong></span>BufferedReader(inputStreamReader); String line = <span style="color: rgba(0, 128, 0, 1)"><strong>""</strong></span>; <span style="color: rgba(0, 0, 128, 1)"><strong>while </strong></span>((line = br.readLine()) != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { } <span style="color: rgba(0, 0, 128, 1)"><strong>if </strong></span>(br != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { br.close(); } <span style="color: rgba(0, 0, 128, 1)"><strong>if </strong></span>(inputStreamReader != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { inputStreamReader.close(); } <span style="color: rgba(0, 0, 128, 1)"><strong>if </strong></span>(errorStream != <span style="color: rgba(0, 0, 128, 1)"><strong>null</strong></span>) { errorStream.close(); } } 

// public static void main(String[] args) { // String videoInputPath = “G:/videos-resources/DFH9X09GR4/video/.mp4”; // String coverOutputPath = “G:/videos-resources/DFH9X09GR4/video/.jpg”; // try { // convetor(“00:00:01”, videoInputPath, 1, coverOutputPath); // } catch (Exception e) {// e.printStackTrace(); // } // } }

==============================================================================================

该工具类其实是通过java来运行Dos界面的命令,有了这个工具类,我们就可以调用该类中的convetor来应对不同的需求,当然,这里只介绍了FFmpeg中的部分功能,如果你想在java中使用FFmpeg的其他功能也很简单,请参考该类,继续重载该类中的convetor方法,将dos界面的命令搬到该类中来运行即可。

&nbsp;

  • 上一篇: jms java教程
  • 下一篇: java ee大学教程
  • 版权声明


    相关文章:

  • jms java教程2025-10-29 18:18:03
  • 评测java自学教程2025-10-29 18:18:03
  • java教程 动脑学院2025-10-29 18:18:03
  • btr教程java程序2025-10-29 18:18:03
  • 兄弟连java教程2025-10-29 18:18:03
  • java ee大学教程2025-10-29 18:18:03
  • java初步教程2025-10-29 18:18:03
  • 488集java教程2025-10-29 18:18:03
  • java ee sdk 安装教程2025-10-29 18:18:03
  • 打砖块游戏java开发教程2025-10-29 18:18:03