Java 实现视频裁剪(附代码) | Java工具类
Posted 剑客·阿良
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 实现视频裁剪(附代码) | Java工具类相关的知识,希望对你有一定的参考价值。
前言
本文提供将视频按照自定义尺寸进行裁剪的Java工具类,一如既往的实用主义。
Maven依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.2</version>
</dependency>
代码
不废话,上代码。
package ai.guiji.csdn.tool;
import cn.hutool.core.util.IdUtil;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import org.bytedeco.javacpp.Loader;
import java.io.File;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
/**
* @Program: csdn @ClassName: CutOutTool @Author: 剑客阿良_ALiang @Date: 2022-01-23 18:27 @Description:
* 裁剪工具 @Version: V1.0
*/
public class CutOutTool
/**
* 视频裁剪
*
* @param videoPath 视频地址
* @param outputDir 临时目录
* @param startX 裁剪起始x坐标
* @param startY 裁剪起始y坐标
* @param weight 裁剪宽度
* @param height 裁剪高度
* @throws Exception 异常
*/
public static String cutOutVideo(
String videoPath,
String outputDir,
Integer startX,
Integer startY,
Integer weight,
Integer height)
throws Exception
List<String> paths = Splitter.on(".").splitToList(videoPath);
String ext = paths.get(paths.size() - 1);
if (!Arrays.asList("mp4", "avi", "flv").contains(ext))
throw new Exception("format error");
String resultPath =
Joiner.on(File.separator).join(Arrays.asList(outputDir, IdUtil.simpleUUID() + "." + ext));
String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
ProcessBuilder builder =
new ProcessBuilder(
ffmpeg,
"-i",
videoPath,
"-vf",
MessageFormat.format(
"crop=0:1:2:3",
String.valueOf(weight),
String.valueOf(height),
String.valueOf(startX),
String.valueOf(startY)),
"-b",
"2000k",
"-y",
"-threads",
"5",
"-preset",
"ultrafast",
"-strict",
"-2",
resultPath);
builder.inheritIO().start().waitFor();
return resultPath;
public static void main(String[] args) throws Exception
System.out.println(
cutOutVideo("C:\\\\Users\\\\yi\\\\Desktop\\\\3.mp4", "C:\\\\Users\\\\yi\\\\Desktop\\\\", 0, 0, 960, 1080));
代码说明:
1、cutOutVideo方法参数分别为视频路径、输出临时目录、起始坐标x值、起始坐标y值、裁剪宽度、裁剪高度。
2、采用uuid作为临时输出唯一id,避免重复。
3、对文件后缀格式做了校验,可以按照需求自行调整。
4、裁剪尺寸不能超出视频分辨率限制,按照需求自行调整。
验证一下
准备的视频如下
执行结果
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 10.2.0 (Rev5, Built by MSYS2 project)
configuration: --prefix=.. --disable-iconv --disable-opencl --disable-sdl2 --disable-bzlib --disable-lzma --disable-linux-perf --enable-shared --enable-version3 --enable-runtime-cpudetect --enable-zlib --enable-libmp3lame --enable-libspeex --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-openssl --enable-libopenh264 --enable-libvpx --enable-libfreetype --enable-libopus --enable-cuda --enable-cuvid --enable-nvenc --enable-libmfx --enable-w32threads --enable-indev=dshow --target-os=mingw32 --cc=\'gcc -m64\' --extra-cflags=-I../include/ --extra-ldflags=-L../lib/ --extra-libs=\'-static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lgcc_eh -lWs2_32 -lcrypt32 -lpthread -lz -lm -Wl,-Bdynamic -lole32 -luuid\'
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from \'C:\\Users\\yi\\Desktop\\3.mp4\':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.45.100
Duration: 00:00:31.02, start: 0.000000, bitrate: 3666 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 3278 kb/s, 24 fps, 24.42 tbr, 19536 tbn, 48.84 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 384 kb/s (default)
Metadata:
handler_name : SoundHandler
Please use -b:a or -b:v, -b is ambiguous
Codec AVOption preset (Set the encoding preset) specified for output file #0 (C:\\Users\\yi\\Desktop\\\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4) has not been used for any stream. The most likely reason is either wrong type (e.g. a video option with no video streams) or that it is a private option of some encoder which was not actually used for any stream.
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> mpeg4 (native))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, mp4, to \'C:\\Users\\yi\\Desktop\\\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4\':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.45.100
Stream #0:0(und): Video: mpeg4 (mp4v / 0x7634706D), yuv420p, 960x1080 [SAR 1:1 DAR 8:9], q=2-31, 2000 kb/s, 24.42 fps, 19536 tbn, 24.42 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc58.91.100 mpeg4
Side data:
cpb: bitrate max/min/avg: 0/0/2000000 buffer size: 0 vbv_delay: N/A
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc58.91.100 aac
frame= 732 fps=251 q=1.6 Lsize= 7575kB time=00:00:30.01 bitrate=2067.5kbits/s dup=13 drop=0 speed=10.3x
video:7087kB audio:470kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.249497%
[aac @ 0000022b33400c40] Qavg: 197.756
C:\\Users\\yi\\Desktop\\\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4
Process finished with exit code 0
结果视频如下
OK,没什么问题!
总结
没什么好总结的,注意尺寸,注意尺寸,注意尺寸。
分享:
当情绪化的狂热平静下来,拥有推理习惯的人就会去为他自己的信仰寻找逻辑支持。——罗素
如果本文对你有帮助的话,点个赞吧,谢谢!!
本人CSDN主页地址:剑客阿良_ALiang的主页
一起学习,一起进步。
以上是关于Java 实现视频裁剪(附代码) | Java工具类的主要内容,如果未能解决你的问题,请参考以下文章