Java實(shí)現(xiàn)視頻自定義裁剪功能
前言
本文提供將視頻按照自定義尺寸進(jìn)行裁剪的Java工具類,一如既往的實(shí)用主義。
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 臨時(shí)目錄
* @param startX 裁剪起始x坐標(biāo)
* @param startY 裁剪起始y坐標(biāo)
* @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方法參數(shù)分別為視頻路徑、輸出臨時(shí)目錄、起始坐標(biāo)x值、起始坐標(biāo)y值、裁剪寬度、裁剪高度。
2、采用uuid作為臨時(shí)輸出唯一id,避免重復(fù)。
3、對(duì)文件后綴格式做了校驗(yàn),可以按照需求自行調(diào)整。
4、裁剪尺寸不能超出視頻分辨率限制,按照需求自行調(diào)整。
驗(yàn)證一下
準(zhǔn)備的視頻如下

執(zhí)行結(jié)果
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結(jié)果視頻如下

到此這篇關(guān)于Java實(shí)現(xiàn)視頻自定義裁剪功能的文章就介紹到這了,更多相關(guān)Java視頻裁剪內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot中使用Spring-data-jpa的配置方法詳解
今天小編就為大家分享一篇關(guān)于Spring Boot中使用Spring-data-jpa的配置方法詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
Java實(shí)現(xiàn)支付寶之第三方支付寶即時(shí)到賬支付功能
這篇文章主要介紹了Java實(shí)現(xiàn)支付寶之第三方支付寶即時(shí)到賬支付功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
關(guān)于JavaEE匿名內(nèi)部類和Lambda表達(dá)式的注意事項(xiàng)
這篇文章主要介紹了關(guān)于JavaEE匿名內(nèi)部類和Lambda表達(dá)式的注意事項(xiàng),匿名內(nèi)部類顧名思義是沒有修飾符甚至沒有名稱的內(nèi)部類,使用匿名內(nèi)部類需要注意哪些地方,我們一起來看看吧2023-03-03
java實(shí)現(xiàn)從方法返回多個(gè)值功能示例
這篇文章主要介紹了java實(shí)現(xiàn)從方法返回多個(gè)值功能,結(jié)合實(shí)例形式分析了集合類、封裝對(duì)象、引用傳遞三種實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
SpringSecurity頁面授權(quán)與登錄驗(yàn)證實(shí)現(xiàn)(內(nèi)存取值與數(shù)據(jù)庫取值)
Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架,本文主要介紹了SpringSecurity頁面授權(quán)與登錄驗(yàn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Spring-MVC異步請(qǐng)求之Servlet異步處理
這篇文章主要介紹了Spring-MVC異步請(qǐng)求之Servlet異步處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01

