小言_互联网的博客

Java 实现视频时间维度剪切 | Java工具类

374人阅读  评论(0)

目录

前言

Maven依赖

代码

总结


前言

本文提供将视频按照时间维度进行剪切的Java工具类,一如既往的实用主义。

Maven依赖


  
  1. <dependency>
  2. <groupId>com.google.guava </groupId>
  3. <artifactId>guava </artifactId>
  4. <version>30.1.1-jre </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.bytedeco </groupId>
  8. <artifactId>javacv-platform </artifactId>
  9. <version>1.5.5 </version>
  10. </dependency>
  11. <dependency>
  12. <groupId>cn.hutool </groupId>
  13. <artifactId>hutool-all </artifactId>
  14. <version>5.5.2 </version>
  15. </dependency>

代码

不废话,上代码。


  
  1. package ai.guiji.csdn.tools;
  2. import cn.hutool.core.util.IdUtil;
  3. import com.google.common.base.Joiner;
  4. import com.google.common.base.Splitter;
  5. import org.bytedeco.javacpp.Loader;
  6. import java.io.File;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. /** @Author 剑客阿良_ALiang @Date 2022/12/27 9:23 @Description: 视频剪切工具 */
  10. public class CutVideoUtils {
  11. /**
  12. * 剪切视频
  13. *
  14. * @param inputVideoPath 输入视频地址
  15. * @param outputDir 输出目录
  16. * @param startTime 起始时间,格式:0:01、10:08
  17. * @param duration 持续时间,单位为:秒
  18. * @return 图片地址
  19. * @throws Exception 异常
  20. */
  21. public static String cut (
  22. String inputVideoPath, String outputDir, String startTime, Integer duration)
  23. throws Exception {
  24. List<String> paths = Splitter.on( ".").splitToList(inputVideoPath);
  25. String ext = paths.get(paths.size() - 1);
  26. if (!Arrays.asList( "mp4", "flv").contains(ext)) {
  27. throw new Exception( "format error");
  28. }
  29. String resultPath =
  30. Joiner.on(File.separator).join(Arrays.asList(outputDir, IdUtil.simpleUUID() + "." + ext));
  31. String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
  32. ProcessBuilder builder =
  33. new ProcessBuilder(
  34. ffmpeg,
  35. "-ss",
  36. startTime,
  37. "-i",
  38. inputVideoPath,
  39. "-t",
  40. String.valueOf(duration),
  41. "-c:v",
  42. "copy",
  43. "-c:a",
  44. "copy",
  45. "-y",
  46. resultPath);
  47. builder.inheritIO().start().waitFor();
  48. return resultPath;
  49. }
  50. public static void main (String[] args) throws Exception {
  51. System.out.println(
  52. cut(
  53. "E:\\360MoveData\\Users\\huyi\\Desktop\\3333333.mp4",
  54. "E:\\360MoveData\\Users\\huyi\\Desktop\\",
  55. "0:10",
  56. 5));
  57. }
  58. }

代码说明:

1、cut方法参数分别为输入视频路径、输出临时目录、剪切起始点、剪切持续时间。

2、起始时间的格式要注意,给了几个格式样式:0:01、10:11,前面为分钟后面为秒。

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.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
 

结果视频信息如下

OK,没什么问题!

总结

没什么好总结的,注意时长区间。

分享

        一个人总有一天会明白,嫉妒是最无用的,而模仿他人无异于自杀。——《人性的弱点》

如果本文对你有帮助的话,点个赞吧,谢谢!!


转载:https://blog.csdn.net/zhiweihongyan1/article/details/128451979
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场