App下載

教你用Java來(lái)實(shí)現(xiàn)把視頻批量轉(zhuǎn)換為gif動(dòng)態(tài)圖

桃花下淺酌 2021-08-10 14:34:48 瀏覽數(shù) (3696)
反饋

現(xiàn)如今短視頻非常火熱,里面的內(nèi)容層出不窮,而且還是新穎奇特。有時(shí)候?qū)τ谀硞€(gè)視頻的感官還不錯(cuò),就想把它存下來(lái)當(dāng)做手機(jī)壁紙或是電腦壁紙。下面,就和大家分享一下如何用Java來(lái)實(shí)現(xiàn)把視頻轉(zhuǎn)換為gif動(dòng)態(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)平臺(tái)的依賴 -->
		<dependency>
			<groupId>ws.schild</groupId>
			<artifactId>jave-nativebin-win64</artifactId>
			<version>3.1.1</version>
		</dependency>
     <!-- linux系統(tǒng)平臺(tái)的依賴 -->
		<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é)果截圖

2021042210290280

再桌面建立video文件夾,將要轉(zhuǎn)換的視頻文件放入進(jìn)去。(gif文件夾可以不建,程序會(huì)自動(dòng)生成)

2021042210290281

原視頻文件 

2021042210290382

轉(zhuǎn)化后的git文件

2021042210290383

測(cè)試結(jié)果

視頻格式為mp4,大小約4.77MB,轉(zhuǎn)為同分辨率,幀率為5的gif文件,大小約4.70MB,轉(zhuǎn)化時(shí)間1s左右。

介紹到此,本篇關(guān)于使用Java來(lái)實(shí)現(xiàn)把視頻轉(zhuǎn)換為gif動(dòng)態(tài)圖的內(nèi)容就結(jié)束了,想要了解更多有趣好玩的Java小程序設(shè)計(jì)的內(nèi)容,可以瀏覽W3Cschool相關(guān)技術(shù)文章,如果各位覺(jué)得還不錯(cuò)的話,還希望能夠多多支持!


0 人點(diǎn)贊