FFmpegUtils.java
8.96 KB
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
package com.glsx.aicar.util;
import io.microshow.rxffmpeg.RxFFmpegCommandList;
/**
* Created by: lixingzhou
* Created Date: 2020/4/16 18:00
* Description:FFmpeg命令集成工具
*/
public class FFmpegUtils {
//视频添加水印 位置标志
public static final int TYPE_SITE_LEFT_TOP = 1;//左上角
public static final int TYPE_SITE_RIGHT_TOP = 2;//右上角
public static final int TYPE_SITE_LEFT_BOTTOM = 3;//左下角
public static final int TYPE_SITE_RIGHT_BOTTOM = 4;//右下角
/**
* 获取视频帧图片
*
* @param videoAbsolutePath 视频地址
* @param startTime 开始时间 eg:第8s开始 00:00:08
* @param duration 抓取时长
* @param r 每秒帧数
* @param imageInfo 输出图片信息 eg:/storage/emulated/0/Video/image/pic_%03d.jpeg
* @param photoSize 图片尺寸 eg:500x420
* @return 命令集
*/
public static String[] getVideoFrameImageCommand(String videoAbsolutePath, String startTime, String duration, String r, String imageInfo, String photoSize) {
RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
//如果目标文件已存在 覆盖目标文件
cmdlist.append("-y");
cmdlist.append("-i");
cmdlist.append(videoAbsolutePath);
cmdlist.append("-ss");
cmdlist.append(startTime);
cmdlist.append("-t");
cmdlist.append(duration);
cmdlist.append("-r");
cmdlist.append(r);
cmdlist.append("-f");
cmdlist.append("image2");
cmdlist.append("-s");
cmdlist.append(photoSize);
cmdlist.append(imageInfo);
return cmdlist.build();
}
/**
* 视频裁剪
*
* @param videoAbsolutePath 源视频路径
* @param startTime 开始时间
* @param duration 截取时长
* @param outputVideoPath 输出文件目录
* @return 命令集
*/
public static String[] videoTailor(String videoAbsolutePath, String startTime, String duration, String outputVideoPath) {
RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
cmdlist.append("-y");
cmdlist.append("-i");
cmdlist.append(videoAbsolutePath);
cmdlist.append("-ss");
cmdlist.append(startTime);
cmdlist.append("-c");
cmdlist.append("copy");
cmdlist.append("-t");
cmdlist.append(duration);
cmdlist.append(outputVideoPath);
return cmdlist.build();
}
/**
* 视频调速 ffmpeg -i TheOrigin.mp4 -vf "setpts=0.25*PTS" UpTheOrigin.mp4
*
* @param inputVideoPath 视频地址
* @param videoSpeed 视频倍速 0.5表示加速两倍 5表示慢速5倍 取值范围[0.25, 4]
* @param musicSpeed 音频倍速 0.5表示慢速1/2 2表示加速2倍 取值范围[0.5, 2]
* @param outputVideoPath 输出视频地址
* @param isVideoWithAudio 视频是否包含音频
* @return 命令集
*/
public static String[] videoSpeedAdjust(String inputVideoPath, String videoSpeed, String musicSpeed, String outputVideoPath, boolean isVideoWithAudio) {
RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
cmdlist.append("-y");
cmdlist.append("-i");
cmdlist.append(inputVideoPath);
if (isVideoWithAudio) {
cmdlist.append("-filter_complex");
cmdlist.append("[0:v]setpts=" + videoSpeed + "*PTS[v];[0:a]atempo=" + musicSpeed + "[a]");
cmdlist.append("-map");
cmdlist.append("[v]");
cmdlist.append("-map");
cmdlist.append("[a]");
} else {
cmdlist.append("-vf");
cmdlist.append("setpts=" + videoSpeed + "*PTS");
}
cmdlist.append(outputVideoPath);
return cmdlist.build();
}
/**
* 视频配乐 ffmpeg -i input1.avi -i input2.mp3 -vcodec copy -acodec copy out.avi
* ffmpeg -y -i /storage/emulated/0/1/input.mp4 -i /storage/emulated/0/1/input.mp3 -filter_complex [0:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=0.2[a0];[1:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=1[a1];[a0][a1]amix=inputs=2:duration=first[aout] -map [aout] -ac 2 -c:v copy -map 0:v:0 -preset superfast /storage/emulated/0/1/result.mp4
*
* @param inputVideoPath 视频地址
* @param musicPath 音频地址
* @param originalVolume 原声音量 取值 0~1 数值越小音量越小
* @param dubVolume 配乐音量 取值 0~1 数值越小音量越小
* @param outputVideoPath 输出文件地址
* @param isVideoWithAudio 视频是否包含音频
* @return 命令集
*/
public static String[] videoMusicConfig(String inputVideoPath, String musicPath, double originalVolume, double dubVolume, String outputVideoPath, boolean isVideoWithAudio) {
RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
cmdlist.append("-y");
cmdlist.append("-i");
cmdlist.append(inputVideoPath);
cmdlist.append("-i");
cmdlist.append(musicPath);
if (isVideoWithAudio) {
cmdlist.append("-filter_complex");
// cmdlist.append("[0:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=0.3[a0];" + //原声音量
// "[1:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=0.7[a1];" + //配乐音量
// "[a0][a1]amix=inputs=2:duration=first[aout]");
cmdlist.append("[0:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=" + originalVolume + "[a0];" + //原声音量
"[1:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=" + dubVolume + "[a1];" + //配乐音量
"[a0][a1]amix=inputs=2:duration=first[aout]");
cmdlist.append("-map");
cmdlist.append("[aout]");
cmdlist.append("-ac");
cmdlist.append("2");
cmdlist.append("-c:v");
cmdlist.append("copy");
cmdlist.append("-map");
cmdlist.append("0:v:0");
cmdlist.append("-preset");
cmdlist.append("superfast");
} else {
cmdlist.append("-c:v");
cmdlist.append("copy");
cmdlist.append("-preset");
cmdlist.append("superfast");
}
cmdlist.append(outputVideoPath);
return cmdlist.build();
}
/**
* 视频压缩 ffmpeg -y -i /storage/emulated/0/1/input.mp4 -b 2097k -r 30 -vcodec libx264 -preset superfast /storage/emulated/0/1/result.mp4
*
* @param inputVideoPath 视频地址
* @param outputVideoPath 输出视频地址
* @return 命令集
*/
public static String[] videoCompress(String inputVideoPath, String outputVideoPath, String crf) {
RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
cmdlist.append("-y");
cmdlist.append("-i");
cmdlist.append(inputVideoPath);
// cmdlist.append("-b");
// cmdlist.append("2097k");
// cmdlist.append("-r");
// cmdlist.append("30");
cmdlist.append("-crf");//-crf: 编码质量,取值范围是0-51,默认值为23,数字越小输出视频的质量越高。
cmdlist.append(crf);//当前由于限制30s视频 故该参数当前写死 值为30大概可以将源视频压缩一半 质量基本有保证
cmdlist.append("-vcodec");
cmdlist.append("libx264");
cmdlist.append("-preset");
cmdlist.append("superfast");
cmdlist.append(outputVideoPath);
return cmdlist.build();
}
/**
* 添加水印图
*
* @param inputVideoPath 视频地址
* @param watermarkImagePath 水印图片路径
* @param site 水印位置 参见:{@link FFmpegUtils#TYPE_SITE_LEFT_TOP}
* @param outputVideoPath 输出视频地址
* @return 命令集
*/
public static String[] addWatermarkImage(String inputVideoPath, String watermarkImagePath, int site, String outputVideoPath) {
RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
cmdlist.append("-y");
cmdlist.append("-i");
cmdlist.append(inputVideoPath);
cmdlist.append("-i");
cmdlist.append(watermarkImagePath);
cmdlist.append("-filter_complex");
String siteType;
switch (site) {
case TYPE_SITE_RIGHT_TOP:
siteType = "overlay=W-w";
break;
case TYPE_SITE_LEFT_BOTTOM:
siteType = "overlay=0:H-h";
break;
case TYPE_SITE_RIGHT_BOTTOM:
siteType = "overlay=W-w:H-h";
break;
case TYPE_SITE_LEFT_TOP:
default:
siteType = "overlay";
break;
}
cmdlist.append(siteType);
cmdlist.append(outputVideoPath);
return cmdlist.build();
}
}