Java用20行代碼實現抖音小視頻批量轉換為gif動態圖
本文主要介紹了Java用20行代碼實現抖音小視頻批量轉換為gif動態圖,分享給大家,具體如下:
效果圖
本功能實現需要用到第三方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:UserstarzanDesktopvideo', 'C:UserstarzanDesktopgif'); } }
運行結果截圖
再桌面建立video文件夾,將要轉換的視頻文件放入進去。(gif文件夾可以不建,程序會自動生成)
原視頻文件
轉化后的git文件
測試結果
視頻格式為mp4,大小約4.77MB,轉為同分辨率,幀率為5的gif文件,大小約4.70MB,轉化時間1s左右。
相關文章《震驚,java僅用30行代碼就實現了視頻轉音頻的批量轉換》
到此這篇關于Java用20行代碼實現抖音小視頻批量轉換為gif動態圖的文章就介紹到這了,更多相關Java小視頻批量轉換為gif內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: