Java用20行代碼實(shí)現(xiàn)抖音小視頻批量轉(zhuǎn)換為gif動態(tài)圖
本文主要介紹了Java用20行代碼實(shí)現(xiàn)抖音小視頻批量轉(zhuǎn)換為gif動態(tài)圖,分享給大家,具體如下:
效果圖
本功能實(shí)現(xiàn)需要用到第三方j(luò)ar包 jave,JAVE 是java調(diào)用FFmpeg的封裝工具。
spring boot項(xiàng)目pom文件中添加以下依賴
<!-- https://mvnrepository.com/artifact/ws.schild/jave-core --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-core</artifactId> <version>3.1.1</version> </dependency> <!-- 以下依賴根據(jù)系統(tǒng)二選一 --> <!-- win系統(tǒng)平臺的依賴 --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-win64</artifactId> <version>3.1.1</version> </dependency> <!-- linux系統(tǒng)平臺的依賴 --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-linux64</artifactId> <version>3.1.1</version> </dependency>
Java單類實(shí)現(xiàn)代碼,復(fù)制到Spring boot項(xiàng)目中,用idea編輯器 主方法運(yùn)行。
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"; /** * 獲得轉(zhuǎn)化后的文件名 * * @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; } /** * 轉(zhuǎn)化音頻格式 * * @param sourceFilePath : 源視頻文件路徑 * @param targetFilePath : 目標(biāo)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(); //設(shè)置視頻屬性 VideoAttributes video = new VideoAttributes(); video.setCodec(outputFormat); //設(shè)置視頻幀率 正常為10 ,值越大越流暢 video.setFrameRate(10); //設(shè)置視頻分辨率 VideoSize targetSize = new VideoSize(sourceSize.getWidth() / 5, sourceSize.getHeight() / 5); video.setSize(targetSize); //設(shè)置轉(zhuǎn)碼屬性 EncodingAttributes attrs = new EncodingAttributes(); attrs.setVideoAttributes(video); // 音頻轉(zhuǎn)換格式類 Encoder encoder = new Encoder(); encoder.encode(mediaObject, target, attrs); System.out.println("轉(zhuǎn)換已完成..."); } catch (EncoderException e) { e.printStackTrace(); } } /** * 批量轉(zhuǎn)化視頻格式 * * @param sourceFolderPath : 源視頻文件夾路徑 * @param targetFolderPath : 目標(biāo)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"); } }
運(yùn)行結(jié)果截圖
再桌面建立video文件夾,將要轉(zhuǎn)換的視頻文件放入進(jìn)去。(gif文件夾可以不建,程序會自動生成)
原視頻文件
轉(zhuǎn)化后的git文件
測試結(jié)果
視頻格式為mp4,大小約4.77MB,轉(zhuǎn)為同分辨率,幀率為5的gif文件,大小約4.70MB,轉(zhuǎn)化時間1s左右。
相關(guān)文章《震驚,java僅用30行代碼就實(shí)現(xiàn)了視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換》
到此這篇關(guān)于Java用20行代碼實(shí)現(xiàn)抖音小視頻批量轉(zhuǎn)換為gif動態(tài)圖的文章就介紹到這了,更多相關(guān)Java小視頻批量轉(zhuǎn)換為gif內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java工程使用ffmpeg進(jìn)行音視頻格式轉(zhuǎn)換的實(shí)現(xiàn)
- java僅用30行代碼就實(shí)現(xiàn)了視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換
- 使用開源項(xiàng)目JAVAE2 進(jìn)行視頻格式轉(zhuǎn)換
- java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻
- Java+Windows+ffmpeg實(shí)現(xiàn)視頻轉(zhuǎn)換功能
- 詳解java調(diào)用ffmpeg轉(zhuǎn)換視頻格式為flv
- java依賴jave-all-deps實(shí)現(xiàn)視頻格式轉(zhuǎn)換
相關(guān)文章
SpringBoot事務(wù)異步調(diào)用引發(fā)的bug解決
本文主要介紹了SpringBoot事務(wù)異步調(diào)用引發(fā)的bug解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06java中建立0-10m的消息(字符串)實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava中建立0-10m的消息(字符串)實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05InteliJ IDEA 設(shè)置eclipse快捷鍵 的圖文教程
本文通過圖文并茂的形式給大家介紹了InteliJ IDEA 設(shè)置eclipse快捷鍵 ,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下2018-06-06SpringBoot?自定義注解實(shí)現(xiàn)涉密字段脫敏
關(guān)于數(shù)據(jù)脫敏,網(wǎng)上的文章都是硬編碼規(guī)則,比如對身份證,手機(jī)號,郵件地址等固定寫法脫敏。本文在此基礎(chǔ)上,拓展動態(tài)從數(shù)據(jù)庫查出涉密關(guān)鍵字執(zhí)行脫敏操作。感興趣的同學(xué)可以參考閱讀2023-03-03SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05SpringBoot實(shí)現(xiàn)自定義Starter的步驟詳解
在SpringBoot中,Starter是一種特殊的依賴,它可以幫助我們快速地集成一些常用的功能,例如數(shù)據(jù)庫連接、消息隊(duì)列、Web框架等。在本文中,我們將介紹如何使用Spring Boot實(shí)現(xiàn)自定義Starter,需要的朋友可以參考下2023-06-06@Transactional和@DS怎樣在事務(wù)中切換數(shù)據(jù)源
這篇文章主要介紹了@Transactional和@DS怎樣在事務(wù)中切換數(shù)據(jù)源問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07