Java操作ffmpeg--截取视频
来源:互联网 发布:郭天祥单片机教材下载 编辑:程序博客网 时间:2024/05/17 01:01
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();}}
0 0
- Java操作ffmpeg--截取视频
- FFmpeg截取视频
- ffmpeg 截取视频
- Windows下使用ffmpeg与java实现截取视频缩略图
- ffmpeg转码、截图、截取视频
- python+ffmpeg截取视频段
- ffmpeg 截取一段视频命令
- ffmpeg截取一段视频中一段视频
- 使用ffmpeg截取视频(包括截图)
- 基于ffmpeg截取视频帧画面
- 基于ffmpeg截取视频帧画面
- 使用FFMPEG进行视频截取与合并
- 基于ffmpeg截取视频帧画面
- ffmpeg进行视频转换和截取
- FFmpeg截取视频生成高清Gif
- FFmpeg 入门(1):截取视频帧
- ffmpeg 多张图片转视频,视频转gif,截取视频,截取视频里的帧
- (工具ffmpeg)视频转换为flv格式,视频截取图片
- Annotation Processing
- java定时器配置
- 使用TextWatcher实现EditView的度分秒控制输入。
- otsu自适应阈值分割的算法描述和opencv实现,及其在肤色检测中的应用
- svn使用
- Java操作ffmpeg--截取视频
- MongoDB与传统数据库的使用区别——批量插入与批量查询
- Android中XML文件的读写
- jxl操作excel表格
- 安卓应用开发Eclipse目录结构
- Java调用批处理文件(.bat)
- 第一次作业(4.C语言)
- SpringAOP实例之简单实现(Annotation形式)
- DB2中使用事件监控器抓取锁超时、死锁信息