欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-17 14:07:56
目录前言maven依赖代码验证一下前言本文提供将视频按照自定义尺寸进行裁剪的java工具类,一如既往的实用主义。maven依赖 ...

前言

本文提供将视频按照自定义尺寸进行裁剪的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、裁剪尺寸不能超出视频分辨率限制,按照需求自行调整。

验证一下

准备的视频如下

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

执行结果

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实现视频自定义裁剪功能的文章就介绍到这了,更多相关java视频裁剪内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!