效果圖

本功能實作需要用到第三方jar包 jave,JAVE 是java呼叫FFmpeg的封裝工具,
spring boot專案pom檔案中添加以下依賴
<!-- https://mvnrepository.com/artifact/ws.schild/jave-core -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-core</artifactId>
<version>3.1.1</version>
</dependency>
<!-- 以下依賴根據系統二選一 -->
<!-- win系統平臺的依賴 -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-win64</artifactId>
<version>3.1.1</version>
</dependency>
<!-- linux系統平臺的依賴 -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-linux64</artifactId>
<version>3.1.1</version>
</dependency>
Java單類實作代碼,復制到Spring boot專案中,用idea編輯器 主方法運行,
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.info.VideoInfo;
import ws.schild.jave.info.VideoSize;
import java.io.File;
import java.util.Arrays;
public class VideoToGIf {
//輸出格式
private static final String outputFormat = "gif";
/**
* 獲得轉化后的檔案名
*
* @param sourceFilePath : 源視頻檔案路徑
* @return
*/
public static String getNewFileName(String sourceFilePath) {
File source = new File(sourceFilePath);
String fileName = source.getName().substring(0, source.getName().lastIndexOf("."));
return fileName + "." + outputFormat;
}
/**
* 轉化音頻格式
*
* @param sourceFilePath : 源視頻檔案路徑
* @param targetFilePath : 目標gif檔案路徑
* @return
*/
public static void transform(String sourceFilePath, String targetFilePath) {
File source = new File(sourceFilePath);
File target = new File(targetFilePath);
try {
//獲得原視頻的解析度
MultimediaObject mediaObject = new MultimediaObject(source);
MultimediaInfo multimediaInfo = mediaObject.getInfo();
VideoInfo videoInfo = multimediaInfo.getVideo();
VideoSize sourceSize = videoInfo.getSize();
//設定視頻屬性
VideoAttributes video = new VideoAttributes();
video.setCodec(outputFormat);
//設定視頻幀率 正常為10 ,值越大越流暢
video.setFrameRate(10);
//設定視頻解析度
VideoSize targetSize = new VideoSize(sourceSize.getWidth() / 5, sourceSize.getHeight() / 5);
video.setSize(targetSize);
//設定轉碼屬性
EncodingAttributes attrs = new EncodingAttributes();
attrs.setVideoAttributes(video);
// 音頻轉換格式類
Encoder encoder = new Encoder();
encoder.encode(mediaObject, target, attrs);
System.out.println("轉換已完成...");
} catch (EncoderException e) {
e.printStackTrace();
}
}
/**
* 批量轉化視頻格式
*
* @param sourceFolderPath : 源視頻檔案夾路徑
* @param targetFolderPath : 目標gif檔案夾路徑
* @return
*/
public static void batchTransform(String sourceFolderPath, String targetFolderPath) {
File sourceFolder = new File(sourceFolderPath);
if (sourceFolder.list().length != 0) {
Arrays.asList(sourceFolder.list()).forEach(e -> {
transform(sourceFolderPath + "\\" + e, targetFolderPath + "\\" + getNewFileName(e));
});
}
}
public static void main(String[] args) {
batchTransform("C:\\Users\\tarzan\\Desktop\\video", "C:\\Users\\tarzan\\Desktop\\gif");
}
}
運行結果截圖

再桌面建立video檔案夾,將要轉換的視頻檔案放入進去,(gif檔案夾可以不建,程式會自動生成)

原視頻檔案

轉化后的git檔案

測驗結果
視頻格式為mp4,大小約4.77MB,轉為同解析度,幀率為5的gif檔案,大小約4.70MB,轉化時間1s左右,
相關文章《震驚,java僅用30行代碼就實作了視頻轉音頻的批量轉換》
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/278850.html
標籤:java
上一篇:GUI三種布局管理器(java)
