前言
本文提供将视频按照自定义尺寸进行裁剪的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;
public class CutOutTool {
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
结果视频如下
到此这篇关于Java实现视频自定义裁剪功能的文章就介绍到这了,更多相关Java视频裁剪内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!