首页 > 作文

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

更新时间:2023-04-04 17:57:56 阅读: 评论:0

目录
前言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.ba.joiner;import com.google.common.ba.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.parator).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",            "-pret",            "ultrafast",            "-strict",            "-2",            resultpath);    builder.inheritio().start().waitfor();    return resultpath;  }  public static void main(string[] args) throws exception {    system.out.println(        cutoutvideo("c:\\urs\\yi\\desktop\.mp4", "c:\\urs\\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.100input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'c:\urs\yi\desktop.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    : soundhandlerplea u -b:a or -b:v, -沙丁鱼的做法b is ambiguouscodec avoption pret (t the encoding pret) specified for output file #0 (c:\urs\yi\desktop\
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-run关于元宵的诗句time-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.100libavcodec     58. 91.100 / 58. 91.100libavformat    58. 45.100 / 58. 45.100libavdevice    58. 10.100 / 58. 10.100libavfilter     7. 85.100 /  7. 85.100libswscale      5.  7.100 /  5.  7.100libswresample   3.  7.100 /  3.  7.100input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'c:\urs\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    : soundhandlerplea u -b:a or -b:v, -b is ambiguouscodec avoption pret (t the encoding pret) specified for output file #0 (c:\urs\yi\desktop\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4) has not been ud 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 ud 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:\urs\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.3x    video:7087kb audio:470kb subtitle:0kb other streams:0kb global headers:0kb muxing overhead: 0.249497%[aac @ 0000022b33400c40] qavg: 197.756c:\urs\yi\desktop\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4process finished with exit code 0
ba24b19db0c47faa2cd25bcaee8a3ed.mp4) has not been ud 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 ud 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:\urs\yi\desktop\
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'libavutil      56. 51.100 / 56. 51.100libavcodec     58. 91.100 / 58. 91.100libavformat    58. 45.100 / 58. 45.100libavdevice    58. 10.100 / 58. 10.100libavfilter     7. 85.100 /  7. 85.100libswscale      5.  7.100 /  5.  7.100libswresample   3.  7.100 /  3.  7.100input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'c:\urs\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    : soundhandlerplea u -b:a or -b:v, -b is ambiguouscodec avoption pret (t the encoding pret) specified for output file #0 (c:\urs\yi\desktop\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4) has not been ud 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 ud 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:\urs\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.3x    video:7087kb audio:470kb subtitle:0kb other streams:0kb global headers:0kb muxing overhead: 0.249497%[aac @ 0000022b33400c40] qavg: 197.756c:\urs\yi\desktop\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4process finished with exit code 0
ba24b19db0c47faa2cd25bcaee8a3ed.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 @ 00000teach的过去式22b33400c40] qavg: 197.756c:\urs\yi\desktop\
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'libavutil      56. 51.100 / 56. 51.100libavcodec     58. 91.100 / 58. 91.100libavformat    58. 45.100 / 58. 45.100libavdevice    58. 10.100 / 58. 10.100libavfilter     7. 85.100 /  7. 85.100libswscale      5.  7.100 /  5.  7.100libswresample   3.  7.100 /  3.  7.100input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'c:\urs\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    : soundhandlerplea u -b:a or -b:v, -b is ambiguousc撤销处分申请书odec avoption pret (t the encoding pret) specified for output file #0 (c:\urs\yi\desktop\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4) has not been ud 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 ud 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:\urs\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.3x    video:7087kb audio:470kb subtitle:0kb other streams:0kb global headers:0kb muxing overhead: 0.249497%[aac @ 0000022b33400c40] qavg: 197.756c:\urs\yi\desktop\\0ba24b19db0c47faa2cd25bcaee8a3ed.mp4process finished with exit code 0
ba24b19db0c47faa2cd25bcaee8a3ed.mp4process finished with exit code 0

结果视频如下

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

本文发布于:2023-04-04 17:57:54,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/7251f068c8d8ce9f543c234ba20c4631.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Java实现视频自定义裁剪功能.doc

本文 PDF 下载地址:Java实现视频自定义裁剪功能.pdf

标签:视频   坐标   代码   自定义
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图