一、依赖
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-win64</artifactId>
<version>3.1.1</version>
</dependency>
<!-- 根据系统二选一 -->
<!-- win系统 -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-core</artifactId>
<version>3.1.1</version>
</dependency>
<!-- linux系统 -->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-linux64</artifactId>
<version>3.1.1</version>
</dependency>
<!-- 图片转gif -->
<dependency>
<groupId>com.madgag</groupId>
<artifactId>animated-gif-lib</artifactId>
<version>1.4</version>
</dependency>
二、工具类
package com.xxx;
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("D:\\video", "D:\\video");
}
}
三、图片转gif工具类
package com.xxx;
import com.madgag.gif.fmsware.AnimatedGifEncoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
/**
* @author :LH
* @date :2022-12-01 9:01
* @description:
*/
public class Path {
public static void main(String[] args) throws Exception {
BufferedImage image1 = ImageIO.read(new File("D:/gif/11.png"));
BufferedImage image2 = ImageIO.read(new File("D:/gif/22.jpg"));
BufferedImage image3 = ImageIO.read(new File("D:/gif/33.jpg"));
BufferedImage image4 = ImageIO.read(new File("D:/gif/44.jpg"));
AnimatedGifEncoder e = new AnimatedGifEncoder();
//生成的图片路径
e.start(new FileOutputStream("D:/gif.gif"));
//图片宽高
e.setSize(300, 190);
//图片之间间隔时间
e.setDelay(400);
//重复次数 0表示无限重复 默认不重复
e.setRepeat(0);
//添加图片
e.addFrame(image1);
e.addFrame(image2);
e.addFrame(image3);
e.addFrame(image4);
e.finish();
}
}
标签:jave,动图,java,gif,ws,File,new,import
From: https://www.cnblogs.com/lhsss9825/p/16940429.html