Java實現(xiàn)視頻時間維度剪切的工具類
前言
本文提供將視頻按照時間維度進行剪切的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.tools; 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.util.Arrays; import java.util.List; /** @Author 劍客阿良_ALiang @Date 2022/12/27 9:23 @Description: 視頻剪切工具 */ public class CutVideoUtils { /** * 剪切視頻 * * @param inputVideoPath 輸入視頻地址 * @param outputDir 輸出目錄 * @param startTime 起始時間,格式:0:01、10:08 * @param duration 持續(xù)時間,單位為:秒 * @return 圖片地址 * @throws Exception 異常 */ public static String cut( String inputVideoPath, String outputDir, String startTime, Integer duration) throws Exception { List<String> paths = Splitter.on(".").splitToList(inputVideoPath); String ext = paths.get(paths.size() - 1); if (!Arrays.asList("mp4", "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, "-ss", startTime, "-i", inputVideoPath, "-t", String.valueOf(duration), "-c:v", "copy", "-c:a", "copy", "-y", resultPath); builder.inheritIO().start().waitFor(); return resultPath; } public static void main(String[] args) throws Exception { System.out.println( cut( "E:\\360MoveData\\Users\\huyi\\Desktop\\3333333.mp4", "E:\\360MoveData\\Users\\huyi\\Desktop\\", "0:10", 5)); } }
代碼說明:
1、cut方法參數(shù)分別為輸入視頻路徑、輸出臨時目錄、剪切起始點、剪切持續(xù)時間。
2、起始時間的格式要注意,給了幾個格式樣式:0:01、10:11,前面為分鐘后面為秒。
3、對文件后綴格式做了校驗,可以按照需求自行調整。
4、剪切的時間不要超過視頻時長。
驗證一下
準備的視頻信息如下
執(zhí)行結果
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 'E:\360MoveData\Users\huyi\Desktop\3333333.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 2022-09-08T12:04:43.000000Z
Hw : 1
:
bitRate : 16000000
:
com.apple.quicktime.artwork: {"data":{"edittime":22,"infoStickerId":"","musicId":"","os":"windows","product":"lv","stickerId":"","videoEffectId":"","videoId":"245ba6f1-f2ab-4d70-bc77-c70ea30c171a","videoParams":{"be":0,"ef":0,"ft":0,"ma":0,"me":0,"mu":0,"re":0,"sp":0,"st":0,"te":0,"t
maxrate : 16000000
:
te_is_reencode : 1
:
encoder : Lavf58.76.100
Duration: 00:00:26.91, start: 0.000000, bitrate: 11898 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 11741 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
creation_time : 2022-09-08T12:04:43.000000Z
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 156 kb/s (default)
Metadata:
creation_time : 2022-09-08T12:04:43.000000Z
handler_name : SoundHandler
Output #0, mp4, to 'E:\360MoveData\Users\huyi\Desktop\\51afc6ecc1824343a779ac66fc43668c.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
te_is_reencode : 1
:
Hw : 1
:
bitRate : 16000000
:
com.apple.quicktime.artwork: {"data":{"edittime":22,"infoStickerId":"","musicId":"","os":"windows","product":"lv","stickerId":"","videoEffectId":"","videoId":"245ba6f1-f2ab-4d70-bc77-c70ea30c171a","videoParams":{"be":0,"ef":0,"ft":0,"ma":0,"me":0,"mu":0,"re":0,"sp":0,"st":0,"te":0,"t
maxrate : 16000000
:
encoder : Lavf58.45.100
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 11741 kb/s, 30 fps, 30 tbr, 15360 tbn, 15360 tbc (default)
Metadata:
creation_time : 2022-09-08T12:04:43.000000Z
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 156 kb/s (default)
Metadata:
creation_time : 2022-09-08T12:04:43.000000Z
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 150 fps=0.0 q=-1.0 Lsize= 6608kB time=00:00:04.97 bitrate=10876.3kbits/s speed= 625x
video:6507kB audio:95kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.082907%
E:\360MoveData\Users\huyi\Desktop\\51afc6ecc1824343a779ac66fc43668c.mp4
Process finished with exit code 0
結果視頻信息如下
到此這篇關于Java實現(xiàn)視頻時間維度剪切的工具類的文章就介紹到這了,更多相關Java視頻時間維度剪切內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
GC調優(yōu)實戰(zhàn)之過早提升Premature?Promotion
這篇文章主要為大家介紹了GC調優(yōu)實戰(zhàn)之過早提升Premature?Promotion2022-01-01