Java实现视频自定义裁剪功能

目录

  • 前言
  • Maven依赖
  • 代码
  • 验证一下

前言 本文提供将视频按照自定义尺寸进行裁剪的Java工具类,一如既往的实用主义。

Maven依赖
com.google.guavaguava30.1.1-jreorg.bytedecojavacv-platform1.5.5cn.hutoolhutool-all5.5.2


代码 不废话,上代码。
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 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、裁剪尺寸不能超出视频分辨率限制,按照需求自行调整。

验证一下 准备的视频如下
Java实现视频自定义裁剪功能
文章图片

执行结果
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developersbuilt 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'libavutil56. 51.100 / 56. 51.100libavcodec58. 91.100 / 58. 91.100libavformat58. 45.100 / 58. 45.100libavdevice58. 10.100 / 58. 10.100libavfilter7. 85.100 /7. 85.100libswscale5.7.100 /5.7.100libswresample3.7.100 /3.7.100Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\yi\Desktop\3.mp4':Metadata:major_brand: isomminor_version: 512compatible_brands: isomiso2avc1mp41encoder: Lavf58.45.100Duration: 00:00:31.02, start: 0.000000, bitrate: 3666 kb/sStream #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: VideoHandlerStream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 384 kb/s (default)Metadata:handler_name: SoundHandlerPlease use -b:a or -b:v, -b is ambiguousCodec 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 helpOutput #0, mp4, to 'C:\Users\yi\Desktop\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4':Metadata:major_brand: isomminor_version: 512compatible_brands: isomiso2avc1mp41encoder: Lavf58.45.100Stream #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: VideoHandlerencoder: Lavc58.91.100 mpeg4Side data:cpb: bitrate max/min/avg: 0/0/2000000 buffer size: 0 vbv_delay: N/AStream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)Metadata:handler_name: SoundHandlerencoder: Lavc58.91.100 aacframe=732 fps=251 q=1.6 Lsize=7575kB time=00:00:30.01 bitrate=2067.5kbits/s dup=13 drop=0 speed=10.3xvideo:7087kB audio:470kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.249497%[aac @ 0000022b33400c40] Qavg: 197.756C:\Users\yi\Desktop\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4Process finished with exit code 0

结果视频如下
Java实现视频自定义裁剪功能
文章图片

【Java实现视频自定义裁剪功能】到此这篇关于Java实现视频自定义裁剪功能的文章就介绍到这了,更多相关Java视频裁剪内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读