Java小视频批量转换为gif Java用20行代码实现抖音小视频批量转换为gif动态图

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java小视频批量转换为gif Java用20行代码实现抖音小视频批量转换为gif动态图

洛阳泰山   2021-04-22 我要评论
想了解Java用20行代码实现抖音小视频批量转换为gif动态图的相关内容吗,洛阳泰山在本文为您仔细讲解Java小视频批量转换为gif的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,视频转换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:\\Users\\tarzan\\Desktop\\video", "C:\\Users\\tarzan\\Desktop\\gif");
    }
 
 
}

运行结果截图

再桌面建立video文件夹,将要转换的视频文件放入进去。(gif文件夹可以不建,程序会自动生成)

原视频文件 

转化后的git文件

测试结果

视频格式为mp4,大小约4.77MB,转为同分辨率,帧率为5的gif文件,大小约4.70MB,转化时间1s左右。

相关文章《震惊,java仅用30行代码就实现了视频转音频的批量转换

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们