溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用Java來實現(xiàn)把視頻批量轉(zhuǎn)換為gif動態(tài)圖

發(fā)布時間:2022-02-28 10:46:44 來源:億速云 閱讀:352 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“怎么用Java來實現(xiàn)把視頻批量轉(zhuǎn)換為gif動態(tài)圖”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“怎么用Java來實現(xiàn)把視頻批量轉(zhuǎn)換為gif動態(tài)圖”吧!

本功能實現(xiàn)需要用到第三方j(luò)ar包 jave,JAVE 是java調(diào)用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>
     <!-- 以下依賴根據(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單類實現(xiàn)代碼,復(fù)制到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";
 
 
    /**
     * 獲得轉(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");
    }
 
 
}

到此,相信大家對“怎么用Java來實現(xiàn)把視頻批量轉(zhuǎn)換為gif動態(tài)圖”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI