千家信息网

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

发表于:2024-11-14 作者:千家信息网编辑
千家信息网最后更新 2024年11月14日,今天就跟大家聊聊有关Java怎么实现视频自定义裁剪功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。前言下面提供将视频按照自定义尺寸进行裁剪的
千家信息网最后更新 2024年11月14日Java怎么实现视频自定义裁剪功能

今天就跟大家聊聊有关Java怎么实现视频自定义裁剪功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

前言

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

Maven依赖

                    com.google.guava            guava            30.1.1-jre                            org.bytedeco            javacv-platform            1.5.5                            cn.hutool            hutool-all            5.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、裁剪尺寸不能超出视频分辨率限制,按照需求自行调整。

验证一下

准备的视频如下

执行结果

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-libopenh364 --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.100Input #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: h364 (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    : 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 (h364 (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     : 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 aacframe=  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.756C:\Users\yi\Desktop\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4Process finished with exit code 0

结果视频如下

看完上述内容,你们对Java怎么实现视频自定义裁剪功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

0