技术专栏
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
<
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");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
<
运行结果截图
再桌面建立video文件夹,将要转换的视频文件放入进去。(gif文件夹可以不建,程序会自动生成)
原视频文件
转化后的git文件
测试结果
视频格式为mp4,大小约4.77MB,转为同分辨率,帧率为5的gif文件,大小约4.70MB,转化时间1s左右。
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
点赞
收藏
评论
打赏
- 分享
- 举报
评论
0个
手气红包

相关专栏
-
浏览量:8583次2020-11-23 18:55:41
-
浏览量:2352次2022-03-11 09:01:37
-
浏览量:1984次2023-11-14 13:37:34
-
浏览量:13608次2020-12-10 20:05:23
-
浏览量:9742次2020-12-09 21:40:17
-
浏览量:4766次2021-04-23 14:10:09
-
浏览量:4786次2021-02-22 13:48:24
-
浏览量:3044次2019-01-18 13:46:24
-
浏览量:617次2023-12-21 10:45:56
-
浏览量:8338次2021-01-07 16:07:41
-
浏览量:1783次2021-01-15 17:16:48
-
浏览量:5389次2021-01-14 16:44:46
-
浏览量:1431次2022-02-20 09:00:45
-
浏览量:1257次2023-06-03 15:58:55
-
浏览量:674次2023-10-12 11:18:32
-
浏览量:1923次2020-06-29 17:22:21
-
浏览量:2000次2018-01-17 10:44:34
-
浏览量:16291次2021-02-07 11:52:24
-
浏览量:1607次2019-10-24 14:50:25
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者
泰山(Tarzan)
您的支持将鼓励我继续创作!
打赏金额:
¥1

¥5

¥10

¥50

¥100

支付方式:

举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注