Java使用FFmpeg处理视频文件的方法教程
前言
本文主要讲述如何使用Java + FFmpeg实现对视频文件的信息提取、码率压缩、分辨率转换等功能;
之前在网上浏览了一大圈Java使用FFmpeg处理音视频的文章,大多都讲的比较简单,楼主在实操过程中踩了很多坑也填了很多坑,希望这份详细的踩坑&填坑指南能帮助到大家;
1. 什么是FFmpeg
2. 开发前准备
在使用Java调用FFmpeg处理音视频之前,需要先安装FFmpeg,安装方法分为两种:
- 引入封装了FFmpeg的开源框架
- 在系统中手动安装FFmpeg
2.1 引入封装了FFmpeg的开源框架
JAVE.jar(官网点我) 是一个封装了FFmpeg的Java框架,在项目中能直接调用它的API来处理音视频文件;
优点:使用方便,直接在项目中引入JAVE.jar即可处理媒体文件,且开发完成后可以随工程一起打包发布,不需要在目标运行环境内手动安装FFmpeg相关的类库
缺点:JAVE.jar最后一次更新是2009年,其封装的FFmpeg版本是09年或更早前的版本,比较老旧,无法使用一些新特性
(当然也可以看看有没有其他比较新的封装了FFmpeg的框架)
Maven坐标如下:
1 2 3 4 5 | <dependency> <groupId>org.ffmpeg</groupId> <artifactId>sdk</artifactId> <version> 1.0 . 2 </version> </dependency> |
2.2 在系统中手动安装FFmpeg
在运行环境中手动安装FFmpeg稍微有一些麻烦,可以百度 windows/mac安装FFmpeg 这样的关键字,根据网上的安装教程将FFmpeg安装到系统中;
懒人链接:Windows安装教程 Mac安装教程
优点:可以直接调用FFmpeg的相关API处理音视频,FFmpeg版本可控
缺点:手动安装较为麻烦,开发环境与目标运行环境都需要先安装好FFmpeg
3. 使用FFmpeg处理音视频
使用JAVE.jar进行开发与直接使用FFmpeg开发的代码有一些不同,这里以直接使用FFmpeg进行开发的代码进行讲解(开发环境MacOS);(使用JAVE的代码、直接使用FFmpeg的代码都会附在文末供大家下载参考)
通过MediaUtil.java类及其依赖的类,你将可以实现:
- 解析源视频的基本信息,包括视频格式、时长、码率等;
- 解析音频、图片的基本信息;
- 将源视频转换成不同分辨率、不同码率、带或不带音频的新视频;
- 抽取源视频中指定时间点的帧画面,来生成一张静态图;
- 抽取源视频中指定时间段的帧画面,来生成一个GIF动态图;
- 截取源视频中的一段来形成一个新视频;
- 抽取源视频中的音频信息,生成单独的MP3文件;
- 对音视频等媒体文件执行自定义的FFmpeg命令;
3.1 代码结构梳理
MediaUtil.java是整个解析程序中的核心类,封装了各种常用的解析方法供外部调用;
MetaInfo.java定义了多媒体数据共有的一些属性,VideoMetaInfo.java MusicMetaInfo.java ImageMetaInfo.java都继承自MetaInfo.java,分别定义了视频、音频、图片数据相关的一些属性;
AnimatedGifEncoder.java LZWEncoder.java NeuQuant.java在抽取视频帧数、制作GIF动态图的时候会使用到;
CrfValueEnum.java 定义了三种常用的FFmpeg压缩视频时使用到的crf值,PresetVauleEnum.java定义了FFmpeg压缩视频时常用的几种压缩速率值;
3.2 MediaUtil.java主程序类解析
3.2.1 使用前需要注意的几点
1、指定正确的FFmpeg程序执行路径
MacOS安装好FFmpeg后,可以在控制台中通过which ffmpeg命令获取FFmpeg程序的执行路径,在调用MediaUtil.java前先通过其 setFFmpegPath() 方法设置好FFmpeg程序在系统中的执行路径,然后才能顺利调用到FFmpeg去解析音视频;
Windows系统下该路径理论上应设置为:FFmpeg可执行程序在系统中的绝对路径(实际情况有待大家补充)
2、指定解析音视频信息时需要的正则表达式
因项目需要解析后缀格式为 .MP4 .WMV .AAC 的视频和音频文件,所以我研究了JAVE.jar底层调用FFmpeg时的解析逻辑后,在MediaUtil.java中设置好了匹配这三种格式的正则表达式供解析时使用(参考程序中的 durationRegex videoStreamRegex musicStreamRegex 这三个表达式值);
注意:如果你需要解析其他后缀格式如 .MKV .MP3 这样的媒体文件时,你很可能需要根据实际情况修改durationRegex videoStreamRegex musicStreamRegex 这三个正则表达式的值,否则可能无法解析出正确的信息;
3、程序中的很多默认值你可以根据实际需要修改,比如视频帧抽取的默认宽度或高度值、时长等等;
3.2.2 MediaUtil.java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 | package media; import lombok.extern.slf4j.Slf4j; import media.domain.ImageMetaInfo; import media.domain.MusicMetaInfo; import media.domain.VideoMetaInfo; import media.domain.gif.AnimatedGifEncoder; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.sql.Time; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 基于FFmpeg内核来编解码音视频信息; * 使用前需手动在运行环境中安装FFmpeg运行程序,然后正确设置FFmpeg运行路径后MediaUtil.java才能正常调用到FFmpeg程序去处理音视频; * * Author: dreamer-1 * * version: 1.0 * */ @Slf4j public class MediaUtil { /** * 可以处理的视频格式 */ public final static String[] VIDEO_TYPE = { "MP4" , "WMV" }; /** * 可以处理的图片格式 */ public final static String[] IMAGE_TYPE = { "JPG" , "JPEG" , "PNG" , "GIF" }; /** * 可以处理的音频格式 */ public final static String[] AUDIO_TYPE = { "AAC" }; /** * 视频帧抽取时的默认时间点,第10s(秒) * (Time类构造参数的单位:ms) */ private static final Time DEFAULT_TIME = new Time( 0 , 0 , 10 ); /** * 视频帧抽取的默认宽度值,单位:px */ private static int DEFAULT_WIDTH = 320 ; /** * 视频帧抽取的默认时长,单位:s(秒) */ private static int DEFAULT_TIME_LENGTH = 10 ; /** * 抽取多张视频帧以合成gif动图时,gif的播放速度 */ private static int DEFAULT_GIF_PLAYTIME = 110 ; /** * FFmpeg程序执行路径 * 当前系统安装好ffmpeg程序并配置好相应的环境变量后,值为ffmpeg可执行程序文件在实际系统中的绝对路径 */ private static String FFMPEG_PATH = "/usr/bin/ffmpeg" ; // /usr/bin/ffmpeg /** * 视频时长正则匹配式 * 用于解析视频及音频的时长等信息时使用; * * (.*?)表示:匹配任何除\r\n之外的任何0或多个字符,非贪婪模式 * */ private static String durationRegex = "Duration: (\\d*?):(\\d*?):(\\d*?)\\.(\\d*?), start: (.*?), bitrate: (\\d*) kb\\/s.*" ; private static Pattern durationPattern; /** * 视频流信息正则匹配式 * 用于解析视频详细信息时使用; */ private static String videoStreamRegex = "Stream #\\d:\\d[\\(]??\\S*[\\)]??: Video: (\\S*\\S$?)[^\\,]*, (.*?), (\\d*)x(\\d*)[^\\,]*, (\\d*) kb\\/s, (\\d*[\\.]??\\d*) fps" ; private static Pattern videoStreamPattern; /** * 音频流信息正则匹配式 * 用于解析音频详细信息时使用; */ private static String musicStreamRegex = "Stream #\\d:\\d[\\(]??\\S*[\\)]??: Audio: (\\S*\\S$?)(.*), (.*?) Hz, (.*?), (.*?), (\\d*) kb\\/s" ;; private static Pattern musicStreamPattern; /** * 静态初始化时先加载好用于音视频解析的正则匹配式 */ static { durationPattern = Pattern.compile(durationRegex); videoStreamPattern = Pattern.compile(videoStreamRegex); musicStreamPattern = Pattern.compile(musicStreamRegex); } /** * 获取当前多媒体处理工具内的ffmpeg的执行路径 * @return */ public static String getFFmpegPath() { return FFMPEG_PATH; } /** * 设置当前多媒体工具内的ffmpeg的执行路径 * @param ffmpeg_path ffmpeg可执行程序在实际系统中的绝对路径 * @return */ public static boolean setFFmpegPath(String ffmpeg_path) { if (StringUtils.isBlank(ffmpeg_path)) { log.error( "--- 设置ffmpeg执行路径失败,因为传入的ffmpeg可执行程序路径为空! ---" ); return false ; } File ffmpegFile = new File(ffmpeg_path); if (!ffmpegFile.exists()) { log.error( "--- 设置ffmpeg执行路径失败,因为传入的ffmpeg可执行程序路径下的ffmpeg文件不存在! ---" ); return false ; } FFMPEG_PATH = ffmpeg_path; log.info( "--- 设置ffmpeg执行路径成功 --- 当前ffmpeg可执行程序路径为: " + ffmpeg_path); return true ; } /** * 测试当前多媒体工具是否可以正常工作 * @return */ public static boolean isExecutable() { File ffmpegFile = new File(FFMPEG_PATH); if (!ffmpegFile.exists()) { log.error( "--- 工作状态异常,因为传入的ffmpeg可执行程序路径下的ffmpeg文件不存在! ---" ); return false ; } List<String> cmds = new ArrayList<>( 1 ); cmds.add( "-version" ); String ffmpegVersionStr = executeCommand(cmds); if (StringUtils.isBlank(ffmpegVersionStr)) { log.error( "--- 工作状态异常,因为ffmpeg命令执行失败! ---" ); return false ; } log.info( "--- 工作状态正常 ---" ); return true ; } /** * 执行FFmpeg命令 * @param commonds 要执行的FFmpeg命令 * @return FFmpeg程序在执行命令过程中产生的各信息,执行出错时返回null */ public static String executeCommand(List<String> commonds) { if (CollectionUtils.isEmpty(commonds)) { log.error( "--- 指令执行失败,因为要执行的FFmpeg指令为空! ---" ); return null ; } LinkedList<String> ffmpegCmds = new LinkedList<>(commonds); ffmpegCmds.addFirst(FFMPEG_PATH); // 设置ffmpeg程序所在路径 log.info( "--- 待执行的FFmpeg指令为:---" + ffmpegCmds); Runtime runtime = Runtime.getRuntime(); Process ffmpeg = null ; try { // 执行ffmpeg指令 ProcessBuilder builder = new ProcessBuilder(); builder.command(ffmpegCmds); ffmpeg = builder.start(); log.info( "--- 开始执行FFmpeg指令:--- 执行线程名:" + builder.toString()); // 取出输出流和错误流的信息 // 注意:必须要取出ffmpeg在执行命令过程中产生的输出信息,如果不取的话当输出流信息填满jvm存储输出留信息的缓冲区时,线程就回阻塞住 PrintStream errorStream = new PrintStream(ffmpeg.getErrorStream()); PrintStream inputStream = new PrintStream(ffmpeg.getInputStream()); errorStream.start(); inputStream.start(); // 等待ffmpeg命令执行完 ffmpeg.waitFor(); // 获取执行结果字符串 String result = errorStream.stringBuffer.append(inputStream.stringBuffer).toString(); // 输出执行的命令信息 String cmdStr = Arrays.toString(ffmpegCmds.toArray()).replace( "," , "" ); String resultStr = StringUtils.isBlank(result) ? "【异常】" : "正常" ; log.info( "--- 已执行的FFmepg命令: ---" + cmdStr + " 已执行完毕,执行结果: " + resultStr); return result; } catch (Exception e) { log.error( "--- FFmpeg命令执行出错! --- 出错信息: " + e.getMessage()); return null ; } finally { if ( null != ffmpeg) { ProcessKiller ffmpegKiller = new ProcessKiller(ffmpeg); // JVM退出时,先通过钩子关闭FFmepg进程 runtime.addShutdownHook(ffmpegKiller); } } } /** * 视频转换 * * 注意指定视频分辨率时,宽度和高度必须同时有值; * * @param fileInput 源视频路径 * @param fileOutPut 转换后的视频输出路径 * @param withAudio 是否保留音频;true-保留,false-不保留 * @param crf 指定视频的质量系数(值越小,视频质量越高,体积越大;该系数取值为0-51,直接影响视频码率大小),取值参考:CrfValueEnum.code * @param preset 指定视频的编码速率(速率越快压缩率越低),取值参考:PresetVauleEnum.presetValue * @param width 视频宽度;为空则保持源视频宽度 * @param height 视频高度;为空则保持源视频高度 */ public static void convertVideo(File fileInput, File fileOutPut, boolean withAudio, Integer crf, String preset, Integer width, Integer height) { if ( null == fileInput || !fileInput.exists()) { throw new RuntimeException( "源视频文件不存在,请检查源视频路径" ); } if ( null == fileOutPut) { throw new RuntimeException( "转换后的视频路径为空,请检查转换后的视频存放路径是否正确" ); } if (!fileOutPut.exists()) { try { fileOutPut.createNewFile(); } catch (IOException e) { log.error( "视频转换时新建输出文件失败" ); } } String format = getFormat(fileInput); if (!isLegalFormat(format, VIDEO_TYPE)) { throw new RuntimeException( "无法解析的视频格式:" + format); } List<String> commond = new ArrayList<String>(); commond.add( "-i" ); commond.add(fileInput.getAbsolutePath()); if (!withAudio) { // 设置是否保留音频 commond.add( "-an" ); // 去掉音频 } if ( null != width && width > 0 && null != height && height > 0 ) { // 设置分辨率 commond.add( "-s" ); String resolution = width.toString() + "x" + height.toString(); commond.add(resolution); } commond.add( "-vcodec" ); // 指定输出视频文件时使用的编码器 commond.add( "libx264" ); // 指定使用x264编码器 commond.add( "-preset" ); // 当使用x264时需要带上该参数 commond.add(preset); // 指定preset参数 commond.add( "-crf" ); // 指定输出视频质量 commond.add(crf.toString()); // 视频质量参数,值越小视频质量越高 commond.add( "-y" ); // 当已存在输出文件时,不提示是否覆盖 commond.add(fileOutPut.getAbsolutePath()); executeCommand(commond); } /** * 视频帧抽取 * 默认抽取第10秒的帧画面 * 抽取的帧图片默认宽度为300px * * 转换后的文件路径以.gif结尾时,默认截取从第10s开始,后10s以内的帧画面来生成gif * * @param videoFile 源视频路径 * @param fileOutPut 转换后的文件路径 */ public static void cutVideoFrame(File videoFile, File fileOutPut) { cutVideoFrame(videoFile, fileOutPut, DEFAULT_TIME); } /** * 视频帧抽取(抽取指定时间点的帧画面) * 抽取的视频帧图片宽度默认为320px * * 转换后的文件路径以.gif结尾时,默认截取从指定时间点开始,后10s以内的帧画面来生成gif * * @param videoFile 源视频路径 * @param fileOutPut 转换后的文件路径 * @param time 指定抽取视频帧的时间点(单位:s) */ public static void cutVideoFrame(File videoFile, File fileOutPut, Time time) { cutVideoFrame(videoFile, fileOutPut, time, DEFAULT_WIDTH); } /** * 视频帧抽取(抽取指定时间点、指定宽度值的帧画面) * 只需指定视频帧的宽度,高度随宽度自动计算 * * 转换后的文件路径以.gif结尾时,默认截取从指定时间点开始,后10s以内的帧画面来生成gif * * @param videoFile 源视频路径 * @param fileOutPut 转换后的文件路径 * @param time 指定要抽取第几秒的视频帧(单位:s) * @param width 抽取的视频帧图片的宽度(单位:px) */ public static void cutVideoFrame(File videoFile, File fileOutPut, Time time, int width) { if ( null == videoFile || !videoFile.exists()) { throw new RuntimeException( "源视频文件不存在,请检查源视频路径" ); } if ( null == fileOutPut) { throw new RuntimeException( "转换后的视频路径为空,请检查转换后的视频存放路径是否正确" ); } VideoMetaInfo info = getVideoMetaInfo(videoFile); if ( null == info) { log.error( "--- 未能解析源视频信息,视频帧抽取操作失败 --- 源视频: " + videoFile); return ; } int height = width * info.getHeight() / info.getWidth(); // 根据宽度计算适合的高度,防止画面变形 cutVideoFrame(videoFile, fileOutPut, time, width, height); } /** * 视频帧抽取(抽取指定时间点、指定宽度值、指定高度值的帧画面) * * 转换后的文件路径以.gif结尾时,默认截取从指定时间点开始,后10s以内的帧画面来生成gif * * @param videoFile 源视频路径 * @param fileOutPut 转换后的文件路径 * @param time 指定要抽取第几秒的视频帧(单位:s) * @param width 抽取的视频帧图片的宽度(单位:px) * @param height 抽取的视频帧图片的高度(单位:px) */ public static void cutVideoFrame(File videoFile, File fileOutPut, Time time, int width, int height) { if ( null == videoFile || !videoFile.exists()) { throw new RuntimeException( "源视频文件不存在,请检查源视频路径" ); } if ( null == fileOutPut) { throw new RuntimeException( "转换后的视频路径为空,请检查转换后的视频存放路径是否正确" ); } String format = getFormat(fileOutPut); if (!isLegalFormat(format, IMAGE_TYPE)) { throw new RuntimeException( "无法生成指定格式的帧图片:" + format); } String fileOutPutPath = fileOutPut.getAbsolutePath(); if (! "GIF" .equals(StringUtils.upperCase(format))) { // 输出路径不是以.gif结尾,抽取并生成一张静态图 cutVideoFrame(videoFile, fileOutPutPath, time, width, height, 1 , false ); } else { // 抽取并生成一个gif(gif由10张静态图构成) String path = fileOutPut.getParent(); String name = fileOutPut.getName(); // 创建临时文件存储多张静态图用于生成gif String tempPath = path + File.separator + System.currentTimeMillis() + "_" + name.substring( 0 , name.indexOf( "." )); File file = new File(tempPath); if (!file.exists()) { file.mkdir(); } try { cutVideoFrame(videoFile, tempPath, time, width, height, DEFAULT_TIME_LENGTH, true ); // 生成gif String images[] = file.list(); for ( int i = 0 ; i < images.length; i++) { images[i] = tempPath + File.separator + images[i]; } createGifImage(images, fileOutPut.getAbsolutePath(), DEFAULT_GIF_PLAYTIME); } catch (Exception e) { log.error( "--- 截取视频帧操作出错 --- 错误信息:" + e.getMessage()); } finally { // 删除用于生成gif的临时文件 String images[] = file.list(); for ( int i = 0 ; i < images.length; i++) { File fileDelete = new File(tempPath + File.separator + images[i]); fileDelete.delete(); } file.delete(); } } } /** * 视频帧抽取(抽取指定时间点、指定宽度值、指定高度值、指定时长、指定单张/多张的帧画面) * * @param videoFile 源视频 * @param path 转换后的文件输出路径 * @param time 开始截取视频帧的时间点(单位:s) * @param width 截取的视频帧图片的宽度(单位:px) * @param height 截取的视频帧图片的高度(单位:px,需要大于20) * @param timeLength 截取的视频帧的时长(从time开始算,单位:s,需小于源视频的最大时长) * @param isContinuty false - 静态图(只截取time时间点的那一帧图片),true - 动态图(截取从time时间点开始,timelength这段时间内的多张帧图) */ private static void cutVideoFrame(File videoFile, String path, Time time, int width, int height, int timeLength, boolean isContinuty) { if (videoFile == null || !videoFile.exists()) { throw new RuntimeException( "源视频文件不存在,源视频路径: " ); } if ( null == path) { throw new RuntimeException( "转换后的文件路径为空,请检查转换后的文件存放路径是否正确" ); } VideoMetaInfo info = getVideoMetaInfo(videoFile); if ( null == info) { throw new RuntimeException( "未解析到视频信息" ); } if (time.getTime() + timeLength > info.getDuration()) { throw new RuntimeException( "开始截取视频帧的时间点不合法:" + time.toString() + ",因为截取时间点晚于视频的最后时间点" ); } if (width <= 20 || height <= 20 ) { throw new RuntimeException( "截取的视频帧图片的宽度或高度不合法,宽高值必须大于20" ); } try { List<String> commond = new ArrayList<String>(); commond.add( "-ss" ); commond.add(time.toString()); if (isContinuty) { commond.add( "-t" ); commond.add(timeLength + "" ); } else { commond.add( "-vframes" ); commond.add( "1" ); } commond.add( "-i" ); commond.add(videoFile.getAbsolutePath()); commond.add( "-an" ); commond.add( "-f" ); commond.add( "image2" ); if (isContinuty) { commond.add( "-r" ); commond.add( "3" ); } commond.add( "-s" ); commond.add(width + "*" + height); if (isContinuty) { commond.add(path + File.separator + "foo-%03d.jpeg" ); } else { commond.add(path); } executeCommand(commond); } catch (Exception e) { log.error( "--- 视频帧抽取过程出错 --- 错误信息: " + e.getMessage()); } } /** * 截取视频中的某一段,生成新视频 * * @param videoFile 源视频路径 * @param outputFile 转换后的视频路径 * @param startTime 开始抽取的时间点(单位:s) * @param timeLength 需要抽取的时间段(单位:s,需小于源视频最大时长);例如:该参数值为10时即抽取从startTime开始之后10秒内的视频作为新视频 */ public static void cutVideo(File videoFile, File outputFile, Time startTime, int timeLength) { if (videoFile == null || !videoFile.exists()) { throw new RuntimeException( "视频文件不存在:" ); } if ( null == outputFile) { throw new RuntimeException( "转换后的视频路径为空,请检查转换后的视频存放路径是否正确" ); } VideoMetaInfo info = getVideoMetaInfo(videoFile); if ( null == info) { throw new RuntimeException( "未解析到视频信息" ); } if (startTime.getTime() + timeLength > info.getDuration()) { throw new RuntimeException( "截取时间不合法:" + startTime.toString() + ",因为截取时间大于视频的时长" ); } try { if (!outputFile.exists()) { outputFile.createNewFile(); } List<String> commond = new ArrayList<String>(); commond.add( "-ss" ); commond.add(startTime.toString()); commond.add( "-t" ); commond.add( "" + timeLength); commond.add( "-i" ); commond.add(videoFile.getAbsolutePath()); commond.add( "-vcodec" ); commond.add( "copy" ); commond.add( "-acodec" ); commond.add( "copy" ); commond.add(outputFile.getAbsolutePath()); executeCommand(commond); } catch (IOException e) { log.error( "--- 视频截取过程出错 ---" ); } } /** * 抽取视频里的音频信息 * 只能抽取成MP3文件 * @param videoFile 源视频文件 * @param audioFile 从源视频提取的音频文件 */ public static void getAudioFromVideo(File videoFile, File audioFile) { if ( null == videoFile || !videoFile.exists()) { throw new RuntimeException( "源视频文件不存在: " ); } if ( null == audioFile) { throw new RuntimeException( "要提取的音频路径为空:" ); } String format = getFormat(audioFile); if (!isLegalFormat(format, AUDIO_TYPE)) { throw new RuntimeException( "无法生成指定格式的音频:" + format + " 请检查要输出的音频文件是否是AAC类型" ); } try { if (!audioFile.exists()) { audioFile.createNewFile(); } List<String> commond = new ArrayList<String>(); commond.add( "-i" ); commond.add(videoFile.getAbsolutePath()); commond.add( "-vn" ); // no video,去除视频信息 commond.add( "-y" ); commond.add( "-acodec" ); commond.add( "copy" ); commond.add(audioFile.getAbsolutePath()); executeCommand(commond); } catch (Exception e) { log.error( "--- 抽取视频中的音频信息的过程出错 --- 错误信息: " + e.getMessage()); } } /** * 解析视频的基本信息(从文件中) * * 解析出的视频信息一般为以下格式: * Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '6.mp4': * Duration: 00:00:30.04, start: 0.000000, bitrate: 19031 kb/s * Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 18684 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc (default) * Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 317 kb/s (default) * * 注解: * Duration: 00:00:30.04【视频时长】, start: 0.000000【视频开始时间】, bitrate: 19031 kb/s【视频比特率/码率】 * Stream #0:0(eng): Video: h264【视频编码格式】 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080【视频分辨率,宽x高】, 18684【视频比特率】 kb/s, 25【视频帧率】 fps, 25 tbr, 25k tbn, 50 tbc (default) * Stream #0:1(eng): Audio: aac【音频格式】 (LC) (mp4a / 0x6134706D), 48000【音频采样率】 Hz, stereo, fltp, 317【音频码率】 kb/s (default) * * @param videoFile 源视频路径 * @return 视频的基本信息,解码失败时返回null */ public static VideoMetaInfo getVideoMetaInfo(File videoFile) { if ( null == videoFile || !videoFile.exists()) { log.error( "--- 解析视频信息失败,因为要解析的源视频文件不存在 ---" ); return null ; } VideoMetaInfo videoInfo = new VideoMetaInfo(); String parseResult = getMetaInfoFromFFmpeg(videoFile); Matcher durationMacher = durationPattern.matcher(parseResult); Matcher videoStreamMacher = videoStreamPattern.matcher(parseResult); Matcher videoMusicStreamMacher = musicStreamPattern.matcher(parseResult); Long duration = 0L; // 视频时长 Integer videoBitrate = 0 ; // 视频码率 String videoFormat = getFormat(videoFile); // 视频格式 Long videoSize = videoFile.length(); // 视频大小 String videoEncoder = "" ; // 视频编码器 Integer videoHeight = 0 ; // 视频高度 Integer videoWidth = 0 ; // 视频宽度 Float videoFramerate = 0F; // 视频帧率 String musicFormat = "" ; // 音频格式 Long samplerate = 0L; // 音频采样率 Integer musicBitrate = 0 ; // 音频码率 try { // 匹配视频播放时长等信息 if (durationMacher.find()) { long hours = ( long )Integer.parseInt(durationMacher.group( 1 )); long minutes = ( long )Integer.parseInt(durationMacher.group( 2 )); long seconds = ( long )Integer.parseInt(durationMacher.group( 3 )); long dec = ( long )Integer.parseInt(durationMacher.group( 4 )); duration = dec * 100L + seconds * 1000L + minutes * 60L * 1000L + hours * 60L * 60L * 1000L; //String startTime = durationMacher.group(5) + "ms"; videoBitrate = Integer.parseInt(durationMacher.group( 6 )); } // 匹配视频分辨率等信息 if (videoStreamMacher.find()) { videoEncoder = videoStreamMacher.group( 1 ); String s2 = videoStreamMacher.group( 2 ); videoWidth = Integer.parseInt(videoStreamMacher.group( 3 )); videoHeight = Integer.parseInt(videoStreamMacher.group( 4 )); String s5 = videoStreamMacher.group( 5 ); videoFramerate = Float.parseFloat(videoStreamMacher.group( 6 )); } // 匹配视频中的音频信息 if (videoMusicStreamMacher.find()) { musicFormat = videoMusicStreamMacher.group( 1 ); // 提取音频格式 //String s2 = videoMusicStreamMacher.group(2); samplerate = Long.parseLong(videoMusicStreamMacher.group( 3 )); // 提取采样率 //String s4 = videoMusicStreamMacher.group(4); //String s5 = videoMusicStreamMacher.group(5); musicBitrate = Integer.parseInt(videoMusicStreamMacher.group( 6 )); // 提取比特率 } } catch (Exception e) { log.error( "--- 解析视频参数信息出错! --- 错误信息: " + e.getMessage()); return null ; } // 封装视频中的音频信息 MusicMetaInfo musicMetaInfo = new MusicMetaInfo(); musicMetaInfo.setFormat(musicFormat); musicMetaInfo.setDuration(duration); musicMetaInfo.setBitRate(musicBitrate); musicMetaInfo.setSampleRate(samplerate); // 封装视频信息 VideoMetaInfo videoMetaInfo = new VideoMetaInfo(); videoMetaInfo.setFormat(videoFormat); videoMetaInfo.setSize(videoSize); videoMetaInfo.setBitRate(videoBitrate); videoMetaInfo.setDuration(duration); videoMetaInfo.setEncoder(videoEncoder); videoMetaInfo.setFrameRate(videoFramerate); videoMetaInfo.setHeight(videoHeight); videoMetaInfo.setWidth(videoWidth); videoMetaInfo.setMusicMetaInfo(musicMetaInfo); return videoMetaInfo; } /** * 获取视频的基本信息(从流中) * * @param inputStream 源视频流路径 * @return 视频的基本信息,解码失败时返回null */ public static VideoMetaInfo getVideoMetaInfo(InputStream inputStream) { VideoMetaInfo videoInfo = new VideoMetaInfo(); try { File file = File.createTempFile( "tmp" , null ); if (!file.exists()) { return null ; } FileUtils.copyInputStreamToFile(inputStream, file); videoInfo = getVideoMetaInfo(file); file.deleteOnExit(); return videoInfo; } catch (Exception e) { log.error( "--- 从流中获取视频基本信息出错 --- 错误信息: " + e.getMessage()); return null ; } } /** * 获取音频的基本信息(从文件中) * @param musicFile 音频文件路径 * @return 音频的基本信息,解码失败时返回null */ public static MusicMetaInfo getMusicMetaInfo(File musicFile) { if ( null == musicFile || !musicFile.exists()) { log.error( "--- 无法获取音频信息,因为要解析的音频文件为空 ---" ); return null ; } // 获取音频信息字符串,方便后续解析 String parseResult = getMetaInfoFromFFmpeg(musicFile); Long duration = 0L; // 音频时长 Integer musicBitrate = 0 ; // 音频码率 Long samplerate = 0L; // 音频采样率 String musicFormat = "" ; // 音频格式 Long musicSize = musicFile.length(); // 音频大小 Matcher durationMacher = durationPattern.matcher(parseResult); Matcher musicStreamMacher = musicStreamPattern.matcher(parseResult); try { // 匹配音频播放时长等信息 if (durationMacher.find()) { long hours = ( long )Integer.parseInt(durationMacher.group( 1 )); long minutes = ( long )Integer.parseInt(durationMacher.group( 2 )); long seconds = ( long )Integer.parseInt(durationMacher.group( 3 )); long dec = ( long )Integer.parseInt(durationMacher.group( 4 )); duration = dec * 100L + seconds * 1000L + minutes * 60L * 1000L + hours * 60L * 60L * 1000L; //String startTime = durationMacher.group(5) + "ms"; musicBitrate = Integer.parseInt(durationMacher.group( 6 )); } // 匹配音频采样率等信息 if (musicStreamMacher.find()) { musicFormat = musicStreamMacher.group( 1 ); // 提取音频格式 //String s2 = videoMusicStreamMacher.group(2); samplerate = Long.parseLong(musicStreamMacher.group( 3 )); // 提取采样率 //String s4 = videoMusicStreamMacher.group(4); //String s5 = videoMusicStreamMacher.group(5); musicBitrate = Integer.parseInt(musicStreamMacher.group( 6 )); // 提取比特率 } } catch (Exception e) { log.error( "--- 解析音频参数信息出错! --- 错误信息: " + e.getMessage()); return null ; } // 封装视频中的音频信息 MusicMetaInfo musicMetaInfo = new MusicMetaInfo(); musicMetaInfo.setFormat(musicFormat); musicMetaInfo.setDuration(duration); musicMetaInfo.setBitRate(musicBitrate); musicMetaInfo.setSampleRate(samplerate); musicMetaInfo.setSize(musicSize); return musicMetaInfo; } /** * 获取音频的基本信息(从流中) * @param inputStream 源音乐流路径 * @return 音频基本信息,解码出错时返回null */ public static MusicMetaInfo getMusicMetaInfo(InputStream inputStream) { MusicMetaInfo musicMetaInfo = new MusicMetaInfo(); try { File file = File.createTempFile( "tmp" , null ); if (!file.exists()) { return null ; } FileUtils.copyInputStreamToFile(inputStream, file); musicMetaInfo = getMusicMetaInfo(file); file.deleteOnExit(); return musicMetaInfo; } catch (Exception e) { log.error( "--- 从流中获取音频基本信息出错 --- 错误信息: " + e.getMessage()); return null ; } } /** * 获取图片的基本信息(从流中) * * @param inputStream 源图片路径 * @return 图片的基本信息,获取信息失败时返回null */ public static ImageMetaInfo getImageInfo(InputStream inputStream) { BufferedImage image = null ; ImageMetaInfo imageInfo = new ImageMetaInfo(); try { image = ImageIO.read(inputStream); imageInfo.setWidth(image.getWidth()); imageInfo.setHeight(image.getHeight()); imageInfo.setSize(Long.valueOf(String.valueOf(inputStream.available()))); return imageInfo; } catch (Exception e) { log.error( "--- 获取图片的基本信息失败 --- 错误信息: " + e.getMessage()); return null ; } } /** * 获取图片的基本信息 (从文件中) * * @param imageFile 源图片路径 * @return 图片的基本信息,获取信息失败时返回null */ public static ImageMetaInfo getImageInfo(File imageFile) { BufferedImage image = null ; ImageMetaInfo imageInfo = new ImageMetaInfo(); try { if ( null == imageFile || !imageFile.exists()) { return null ; } image = ImageIO.read(imageFile); imageInfo.setWidth(image.getWidth()); imageInfo.setHeight(image.getHeight()); imageInfo.setSize(imageFile.length()); imageInfo.setFormat(getFormat(imageFile)); return imageInfo; } catch (Exception e) { log.error( "--- 获取图片的基本信息失败 --- 错误信息: " + e.getMessage()); return null ; } } /** * 检查文件类型是否是给定的类型 * @param inputFile 源文件 * @param givenFormat 指定的文件类型;例如:{"MP4", "AVI"} * @return */ public static boolean isGivenFormat(File inputFile, String[] givenFormat) { if ( null == inputFile || !inputFile.exists()) { log.error( "--- 无法检查文件类型是否满足要求,因为要检查的文件不存在 --- 源文件: " + inputFile); return false ; } if ( null == givenFormat || givenFormat.length <= 0 ) { log.error( "--- 无法检查文件类型是否满足要求,因为没有指定的文件类型 ---" ); return false ; } String fomat = getFormat(inputFile); return isLegalFormat(fomat, givenFormat); } /** * 使用FFmpeg的"-i"命令来解析视频信息 * @param inputFile 源媒体文件 * @return 解析后的结果字符串,解析失败时为空 */ public static String getMetaInfoFromFFmpeg(File inputFile) { if (inputFile == null || !inputFile.exists()) { throw new RuntimeException( "源媒体文件不存在,源媒体文件路径: " ); } List<String> commond = new ArrayList<String>(); commond.add( "-i" ); commond.add(inputFile.getAbsolutePath()); String executeResult = MediaUtil.executeCommand(commond); return executeResult; } /** * 检测视频格式是否合法 * @param format * @param formats * @return */ private static boolean isLegalFormat(String format, String formats[]) { for (String item : formats) { if (item.equals(StringUtils.upperCase(format))) { return true ; } } return false ; } /** * 创建gif * * @param image 多个jpg文件名(包含路径) * @param outputPath 生成的gif文件名(包含路径) * @param playTime 播放的延迟时间,可调整gif的播放速度 */ private static void createGifImage(String image[], String outputPath, int playTime) { if ( null == outputPath) { throw new RuntimeException( "转换后的GIF路径为空,请检查转换后的GIF存放路径是否正确" ); } try { AnimatedGifEncoder encoder = new AnimatedGifEncoder(); encoder.setRepeat( 0 ); encoder.start(outputPath); BufferedImage src[] = new BufferedImage[image.length]; for ( int i = 0 ; i < src.length; i++) { encoder.setDelay(playTime); // 设置播放的延迟时间 src[i] = ImageIO.read( new File(image[i])); // 读入需要播放的jpg文件 encoder.addFrame(src[i]); // 添加到帧中 } encoder.finish(); } catch (Exception e) { log.error( "--- 多张静态图转换成动态GIF图的过程出错 --- 错误信息: " + e.getMessage()); } } /** * 获取指定文件的后缀名 * @param file * @return */ private static String getFormat(File file) { String fileName = file.getName(); String format = fileName.substring(fileName.indexOf( "." ) + 1 ); return format; } /** * 在程序退出前结束已有的FFmpeg进程 */ private static class ProcessKiller extends Thread { private Process process; public ProcessKiller(Process process) { this .process = process; } @Override public void run() { this .process.destroy(); log.info( "--- 已销毁FFmpeg进程 --- 进程名: " + process.toString()); } } /** * 用于取出ffmpeg线程执行过程中产生的各种输出和错误流的信息 */ static class PrintStream extends Thread { InputStream inputStream = null ; BufferedReader bufferedReader = null ; StringBuffer stringBuffer = new StringBuffer(); public PrintStream(InputStream inputStream) { this .inputStream = inputStream; } @Override public void run() { try { if ( null == inputStream) { log.error( "--- 读取输出流出错!因为当前输出流为空!---" ); } bufferedReader = new BufferedReader( new InputStreamReader(inputStream)); String line = null ; while ((line = bufferedReader.readLine()) != null ) { log.info(line); stringBuffer.append(line); } } catch (Exception e) { log.error( "--- 读取输入流出错了!--- 错误信息:" + e.getMessage()); } finally { try { if ( null != bufferedReader) { bufferedReader.close(); } if ( null != inputStream) { inputStream.close(); } } catch (IOException e) { log.error( "--- 调用PrintStream读取输出流后,关闭流时出错!---" ); } } } } } |
3.2.3 踩坑&填坑
1、在Linux等服务器上部署Java程序进行视频压缩时,多注意一下运行账号的权限问题,有时候可能是由于运行程序没有足够的文件操作权限,导致压缩过程失败;
2、第一版程序上线后,偶尔会出现这样的问题:
调用MediaUtil.java进行视频压缩过程中,整个程序突然“卡住”,后台也没有日志再打印出来,此时整个压缩过程还没有完成,像是线程突然阻塞住了;
经过多番查找,发现Java调用FFmpeg时,实际是在JVM里产生一个子进程来执行压缩过程,这个子进程与JVM建立三个通道链接(包括标准输入、标准输出、标准错误流),在压缩过程中,实际会不停地向标准输出和错误流中写入信息;
因为本地系统对标准输出及错误流提供的缓冲区大小有限,当写入标准输出和错误流的信息填满缓冲区时,执行压缩的进程就会阻塞住;
所以在压缩过程中,需要单独创建两个线程不停读取标准输出及错误流中的信息,防止整个压缩进程阻塞;(参考MediaUtil.java中的 executeCommand() 方法中的 errorStream 和 inputStream 这两个内部类实例的操作)
3.3 在CentOS服务器安装FFmpeg指南
因项目最后部署在CentOS服务器上,需提前在服务器上安装好FFmpeg程序,这过程中也踩了不少坑,针对此写了另一篇总结文章,参考这里 点我哦
4. 源码下载
这里提供两种版本的源码供大家下载参考:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了