Java操作ffmpeg--截取视频
Java操作ffmpeg截取视频指定时间的内容
src:原文件路径 dest:生成文件路径 start:开始时间(格式:xx:xx:xx) time:截取多少时间(格式:xx:xx:xx,如00:00:40 为40秒) event:默认填null 完成后事件
代码如下:
public static boolean interceptVideo(String src, String dest,String start,String time,AfterVideoConvertEvent event) {FfmpegCommandBuilder fcb = new FfmpegCommandBuilder(); fcb.addFfmpegCommand();fcb.addCommand("-ss");fcb.addCommand(start);fcb.addCommand("-t");fcb.addCommand(time);fcb.addInputFileCommand(src);fcb.addCommand("-vcodec");//fcb.addCommand("copy");fcb.addCommand("h264"); //指定编码为播放器可支持的编码 无特殊要求填copy copy表示复制原编码fcb.addCommand("-acodec");fcb.addCommand("copy");fcb.addCommand(dest);execute(fcb.getCommandList(), src, dest, false, event);return true;}
package net.pubone.framework.media;import java.util.ArrayList;
import java.util.List;import net.pubone.framework.kit.StrKit;import com.jfinal.kit.PathKit;public class FfmpegCommandBuilder {public static final String RESOLUTION_SUPERDEFINITION = "spd";public static final String RESOLUTION_HIGHDEFINITION = "hd";public static final String RESOLUTION_SIMPLEDEFINITION = "smd";public static final int WATERMARKPOSITION_LEFTTOP = 1;public static final int WATERMARKPOSITION_RIGHTTOP = 2;public static final int WATERMARKPOSITION_LEFTBOTTOM = 3;public static final int WATERMARKPOSITION_RIGHTBOTTOM = 4;public FfmpegCommandBuilder() {}public static enum VideoWatermarkPosition {LeftTop, RightTop, LeftBottom, RightBottom;public static VideoWatermarkPosition getValue(int position) {switch (position) {case 1:return LeftTop;case 2:return RightTop;case 3:return LeftBottom;case 4:return RightBottom;}return LeftTop;}}public static enum VideoResolution {SuperDefinition, HighDefinition, SimpleDefinition;public static VideoResolution getValue(String resolution) {if ("spd".equalsIgnoreCase(resolution))return SuperDefinition;if ("hd".equalsIgnoreCase(resolution)) {return HighDefinition;}return SimpleDefinition;}}private List<String> commands = new ArrayList<String>();public List<String> getCommandList() {return this.commands;}public void addFfmpegCommand() {addFfmpegCommand(null);}public void addFfmpegCommand(String ffmpegPath) {String filename = "ffmpeg";if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0) {if (StrKit.isEmpty(ffmpegPath)) {filename = "\"" + PathKit.getWebRootPath() + "/tools/ffmpeg.exe\"";} else {filename = ffmpegPath;}}this.commands.add(filename);}public void addCommand(String command) {this.commands.add(command);}public void addInputFileCommand(String filePath) {this.commands.add("-i");this.commands.add(filePath);}public void addFormatCommand(String format) {this.commands.add("-f");this.commands.add(format);}public void addOutputFileCommand(String filePath) {this.commands.add(filePath);}public void addOverwriteCommand() {this.commands.add("-y");}public void addSimpleAudioCommand() {this.commands.add("-acodec");this.commands.add("libvo_aacenc");this.commands.add("-ab");this.commands.add("128");this.commands.add("-ac");this.commands.add("1");this.commands.add("-ar");this.commands.add("22050");}public void addVideoBitRateCommand(long bitrate) {this.commands.add("-b");this.commands.add(bitrate + "k");}public void addVideoConvertCommand(VideoResolution vr) {this.commands.add("-vcodec");this.commands.add("h264");this.commands.add("-r");this.commands.add("25");if (VideoResolution.SuperDefinition == vr) {this.commands.add("-qscale");this.commands.add("4");} else if (VideoResolution.HighDefinition == vr) {addVideoBitRateCommand(768L);} else {addVideoBitRateCommand(384L);}}public void addWatermarkCommand(String imageName, VideoWatermarkPosition position) {if (StrKit.isEmpty(imageName)) {return;}String positionParam = "";if (position == VideoWatermarkPosition.RightTop)positionParam = "movie=" + imageName + " [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out]";if (position == VideoWatermarkPosition.LeftBottom)positionParam = "movie=" + imageName + " [watermark]; [in][watermark] overlay=10:main_h-overlay_h-10 [out]";if (position == VideoWatermarkPosition.RightBottom) {positionParam = "movie=" + imageName + " [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]";} else {positionParam = "movie=" + imageName + " [watermark]; [in][watermark] overlay=10:10 [out]";}this.commands.add("-vf");this.commands.add(positionParam);}public void addCaptureCommand(int captureSecond, int width, int height) {this.commands.add("-r");this.commands.add("1");this.commands.add("-f");this.commands.add("image2");this.commands.add("-ss");int s = captureSecond % 60;int m = captureSecond / 60 % 60;int h = captureSecond / 60 / 60 % 60;this.commands.add(h + ":" + m + ":" + s);this.commands.add("-t");this.commands.add("00:00:01");this.commands.add("-s");if (width == 0) {width = 320;}if (height == 0) {height = 240;}this.commands.add(width + "x" + height);}@Overridepublic String toString() {StringBuilder sb = new StringBuilder();for (String command : this.commands) {sb.append(command).append(" ");}return sb.toString();}
}
Java操作ffmpeg--截取视频相关推荐
- ffmpeg入门及java操作ffmpeg对视频进行处理
一.ffmpeg 1.简介 FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件).它提供了录制.转换以及流化音视频的完整解决方案.它包含了非 ...
- java使用ffmpeg截取视频某个时间点的截图
1.软件安装 mac brew install ffmpeg 安装后路径:"/usr/local/Cellar/ffmpeg/4.0.1/bin ffmpeg –version 查看版本 l ...
- java使用ffmpeg截取视频作为封面
转自 https://blog.csdn.net/zhaowen25/article/details/39674029 网盘 https://pan.baidu.com/s/1o7YplUE 转自 ...
- Java操作ffmpeg为视频添加音乐
最近学习仿抖音微信小程序遇到一个坑,视频中使用以下语句为视频添加背景音乐 ffmpeg.exe -i input.mp4 -i 音乐.mp3 -t 7 -y 新视频.mp4 发现并不管用. 自己将其改 ...
- java操作ffmpeg为视频添加背景音乐
最近学习仿抖音微信小程序遇到一个坑,视频中使用以下语句为视频添加背景音乐 ffmpeg.exe -i input.mp4 -i 音乐.mp3 -t 7 -y 新视频.mp4 ,然而我怎么尝试都不行,上 ...
- Java 实现分段截取视频 生成gif图 使用ffmpeg操作
Java 实现分段截取视频 生成gif图 使用ffmpeg操作 前言 核心代码 调用的抽象方法,包含逻辑处理 通过ffmpeg获取视频时长 合成gif图 删除文件夹 最终的调用 如果有更好的方式,请务 ...
- Java使用FFmpeg处理视频文件指南
https://www.cnblogs.com/Dreamer-1/p/10394011.html Java使用FFmpeg处理视频文件指南 本文主要讲述如何使用Java + FFmpeg实现对视频文 ...
- php ffmpeg截图,php ffmpeg截取视频第一帧保存为图片的方法
php ffmpeg截取视频第一帧保存为图片的方法 $xiangmupath = $this->getxiangmupath(); $filename = 'chengshi'; $cmd = ...
- Java通过FFMPEG获取视频时长
2019独角兽企业重金招聘Python工程师标准>>> Java通过FFMPEG获取视频时长 详见https://www.yz1618.cn/view/19 转载于:https:// ...
最新文章
- Python入门学习方法有哪些?
- 【计算机网络】计算机网络 OSI 参考模型 与 TCP/IP 参考模型 对比
- Linux下which、whereis、locate、find 命令的区别
- python ‘%r‘或者‘{!r}‘的意思
- 使用JIRA搭建企业问题跟踪系统(转)
- 【SAS NOTE】数组
- 面向 Android* Jelly Bean 4.3 的英特尔® 凌动™ x86 映像安装指南 - 推荐
- 找找看XSS漏洞!!!
- centos7按报错dracut
- 2016阿里技术论坛,阿里技术大神的互联网趋势分享
- 解题报告 poj 1087
- linux下挂载移动硬盘(ntfs格式)
- 学习笔记(1):JDBC连接池
- 【工赋开发者社区】产业互联网和工业互联网的区别
- 简单无迹kalman的matlab程序,卡尔曼滤波原理及应用——MATLAB仿真
- 动态添加 data 属性的时候 发现某一个值无法取到
- PTA - 数据库合集4
- echarts使用rich设置显示数据label颜色
- Delphi 获取系统时间分隔符
- Python、Java、Scala、Go Package对照表