-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFFMpegTest.java
More file actions
144 lines (130 loc) · 4.23 KB
/
FFMpegTest.java
File metadata and controls
144 lines (130 loc) · 4.23 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
package com;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* FFMPEG获取视频时长、复制视频源、获取每一秒的截屏
* @author dzw
*
*/
public class FFMpegTest {
static String prfix_ffmpeg = "D:\\ffmpeg\\bin\\ffmpeg";
public static void main(String[] args) {
int i = getVideoTime("D:\\ffmpeg\\bin\\aaa.mp4", prfix_ffmpeg);
FFMpegTest.coverterVideo("D:\\ffmpeg\\bin\\cc745615e273d51a33ed5795afd79d01.mp4", "D:\\\\ffmpeg\\\\bin\\\\aaa.mp4",
i);
}
public static void coverterVideo(String in, String out, Integer times) {
StringBuilder commond = new StringBuilder();
commond.append(prfix_ffmpeg);
commond.append(" -i ");
commond.append(in);
commond.append(" -vcodec copy -acodec copy -f mp4 ");
commond.append(out);
System.out.println(commond.toString());
Process pro = null;
Process pro1 = null;
try {
if (!new File(out).exists()) {
System.out.println("***********coverter begin*********");
pro = Runtime.getRuntime().exec(commond.toString());
System.out.println(
"***********" + new SimpleDateFormat("yy-MM-dd HH:MM:ss").format(new Date()) + "***********");
pro.waitFor();
System.out.println(
"***********" + new SimpleDateFormat("yy-MM-dd HH:MM:ss").format(new Date()) + "***********");
System.out.println("**********coverter finished!!!*********");
}
// pro.destroy();
if (new File(out).exists()) {
System.out.println("*********************截图开始***********************");
for (int i = 0; i < times; i++) {
StringBuilder commond1 = new StringBuilder();
commond1.append(prfix_ffmpeg);
commond1.append(" -i ");
commond1.append(out);
commond1.append(" -y -f image2 -ss ");
commond1.append(i);
commond1.append(" -vframes 1 ");
commond1.append("-s 350*240 ");
commond1.append("D:\\ffmpeg\\bin\\imgs\\");
commond1.append(i);
commond1.append(".jpg");
pro1 = Runtime.getRuntime().exec(commond1.toString());
pro1.waitFor();
}
System.out.println("*********************截图结束***********************");
}
} catch (Exception e) {
e.printStackTrace();
try {
pro.getErrorStream().close();
pro.destroy();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
/**
* 获取视频总时间
*
* @param viedo_path
* 视频路径
* @param ffmpeg_path
* ffmpeg路径
* @return
*/
public static int getVideoTime(String video_path, String ffmpeg_path) {
List<String> commands = new java.util.ArrayList<String>();
commands.add(ffmpeg_path);
commands.add("-i");
commands.add(video_path);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commands);
final Process p = builder.start();
// 从输入流中读取视频信息
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
// 从视频信息中解析时长
String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
Pattern pattern = Pattern.compile(regexDuration);
Matcher m = pattern.matcher(sb.toString());
if (m.find()) {
int time = getTimelen(m.group(1));
System.err
.println(video_path + ",视频时长:" + time + ", 开始时间:" + m.group(2) + ",比特率:" + m.group(3) + "kb/s");
return time;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
// 格式:"00:00:10.68"
private static int getTimelen(String timelen) {
int min = 0;
String strs[] = timelen.split(":");
if (strs[0].compareTo("0") > 0) {
min += Integer.valueOf(strs[0]) * 60 * 60;// 秒
}
if (strs[1].compareTo("0") > 0) {
min += Integer.valueOf(strs[1]) * 60;
}
if (strs[2].compareTo("0") > 0) {
min += Math.round(Float.valueOf(strs[2]));
}
return min;
}
}