代码片段(1)[全屏查看所有代码]
1. [代码]java调用FFmpeg及mencoder转换视频为FLV并截图
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
|
Conver.java
package
com.ll19.flv;
public
class
Conver {
public
void
run() {
try
{
// 转换并截图
String filePath =
"D:\\video\\old\\test.avi"
;
ConverVideo cv =
new
ConverVideo(filePath);
cv.beginConver();
// 仅截图
// ProcessFlvImg pfi = new ProcessFlvImg();
// pfi.processImg("D:\\video\\old\\test.avi");
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
static
void
main(String args[]) {
Conver c =
new
Conver();
c.run();
}
}
ConverVideo.java
package
com.ll19.flv;
import
java.io.File;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.Date;
import
java.util.List;
public
class
ConverVideo {
private
Date dt;
private
long
begintime;
private
String PATH;
private
String filerealname;
// 文件名 不包括扩展名
private
String filename;
// 包括扩展名
private
String videofolder =
"D:\\video\\other\\"
;
// 别的格式视频的目录
private
String flvfolder =
"D:\\video\\flv\\"
;
// flv视频的目录
private
String ffmpegpath =
"D:\\video\\FFmpeg\\bin\\ffmpeg.exe"
;
// ffmpeg.exe的目录
private
String mencoderpath =
"D:\\video\\mencoder\\"
;
// mencoder的目录
private
String videoRealPath =
"D:\\video\\flv\\"
;
// 截图的视频目录;
private
String imageRealPath =
"D:\\video\\img\\"
;
// 截图的存放目录
public
ConverVideo() {
}
public
ConverVideo(String path) {
PATH = path;
}
public
String getPATH() {
return
PATH;
}
public
void
setPATH(String path) {
PATH = path;
}
public
boolean
beginConver() {
File fi =
new
File(PATH);
filename = fi.getName();
filerealname = filename.substring(
0
, filename.lastIndexOf(
"."
))
.toLowerCase();
System.out.println(
"----接收到文件("
+ PATH
+
")需要转换-------------------------- "
);
if
(!checkfile(PATH)) {
System.out.println(PATH +
"文件不存在"
+
" "
);
return
false
;
}
dt =
new
Date();
begintime = dt.getTime();
System.out
.println(
"----开始转文件("
+ PATH +
")-------------------------- "
);
if
(process()) {
Date dt2 =
new
Date();
System.out.println(
"转换成功 "
);
long
endtime = dt2.getTime();
long
timecha = (endtime - begintime);
String totaltime = sumTime(timecha);
System.out.println(
"共用了:"
+ totaltime +
" "
);
if
(processImg()) {
System.out.println(
"截图成功了 "
);
}
else
{
System.out.println(
"截图不成功了 "
);
}
PATH =
null
;
return
true
;
}
else
{
PATH =
null
;
return
false
;
}
}
public
boolean
processImg() {
List commend =
new
java.util.ArrayList();
commend.add(ffmpegpath);
commend.add(
"-i"
);
commend.add(videoRealPath + filerealname +
".flv"
);
commend.add(
"-y"
);
commend.add(
"-f"
);
commend.add(
"image2"
);
commend.add(
"-ss"
);
commend.add(
"38"
);
commend.add(
"-t"
);
commend.add(
"0.001"
);
commend.add(
"-s"
);
commend.add(
"320x240"
);
commend.add(imageRealPath + filerealname +
".jpg"
);
try
{
ProcessBuilder builder =
new
ProcessBuilder();
builder.command(commend);
builder.start();
return
true
;
}
catch
(Exception e) {
e.printStackTrace();
return
false
;
}
}
private
boolean
process() {
int
type = checkContentType();
boolean
status =
false
;
if
(type ==
0
) {
// status = processFLV(PATH);// 直接将文件转为flv文件
status = processFLV(PATH);
}
else
if
(type ==
1
) {
String avifilepath = processAVI(type);
if
(avifilepath ==
null
)
return
false
;
// avi文件没有得到
else
{
System.out.println(
"kaishizhuang"
);
status = processFLV(avifilepath);
// 将avi转为flv
}
}
return
status;
}
private
int
checkContentType() {
String type = PATH.substring(PATH.lastIndexOf(
"."
) +
1
, PATH.length())
.toLowerCase();
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if
(type.equals(
"avi"
)) {
return
0
;
}
else
if
(type.equals(
"mpg"
)) {
return
0
;
}
else
if
(type.equals(
"wmv"
)) {
return
0
;
}
else
if
(type.equals(
"3gp"
)) {
return
0
;
}
else
if
(type.equals(
"mov"
)) {
return
0
;
}
else
if
(type.equals(
"mp4"
)) {
return
0
;
}
else
if
(type.equals(
"asf"
)) {
return
0
;
}
else
if
(type.equals(
"asx"
)) {
return
0
;
}
else
if
(type.equals(
"flv"
)) {
return
0
;
}
// 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
// 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
else
if
(type.equals(
"wmv9"
)) {
return
1
;
}
else
if
(type.equals(
"rm"
)) {
return
1
;
}
else
if
(type.equals(
"rmvb"
)) {
return
1
;
}
return
9
;
}
private
boolean
checkfile(String path) {
File file =
new
File(path);
if
(!file.isFile()) {
return
false
;
}
else
{
return
true
;
}
}
// 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
private
String processAVI(
int
type) {
List commend =
new
java.util.ArrayList();
commend.add(mencoderpath);
commend.add(PATH);
commend.add(
"-oac"
);
commend.add(
"mp3lame"
);
commend.add(
"-lameopts"
);
commend.add(
"preset=64"
);
commend.add(
"-ovc"
);
commend.add(
"xvid"
);
commend.add(
"-xvidencopts"
);
commend.add(
"bitrate=600"
);
commend.add(
"-of"
);
commend.add(
"avi"
);
commend.add(
"-o"
);
commend.add(videofolder + filerealname +
".avi"
);
// 命令类型:mencoder 1.rmvb -oac mp3lame -lameopts preset=64 -ovc xvid
// -xvidencopts bitrate=600 -of avi -o rmvb.avi
try
{
ProcessBuilder builder =
new
ProcessBuilder();
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
return
videofolder + filerealname +
".avi"
;
}
catch
(Exception e) {
e.printStackTrace();
return
null
;
}
}
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private
boolean
processFLV(String oldfilepath) {
if
(!checkfile(PATH)) {
System.out.println(oldfilepath +
" is not file"
);
return
false
;
}
List commend =
new
java.util.ArrayList();
commend.add(ffmpegpath);
commend.add(
"-i"
);
commend.add(oldfilepath);
commend.add(
"-ab"
);
commend.add(
"64"
);
commend.add(
"-acodec"
);
commend.add(
"mp3"
);
commend.add(
"-ac"
);
commend.add(
"2"
);
commend.add(
"-ar"
);
commend.add(
"22050"
);
commend.add(
"-b"
);
commend.add(
"230"
);
commend.add(
"-r"
);
commend.add(
"24"
);
commend.add(
"-y"
);
commend.add(flvfolder + filerealname +
".flv"
);
try
{
ProcessBuilder builder =
new
ProcessBuilder();
String cmd = commend.toString();
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
deleteFile(oldfilepath);
return
true
;
}
catch
(Exception e) {
e.printStackTrace();
return
false
;
}
}
public
int
doWaitFor(Process p) {
InputStream in =
null
;
InputStream err =
null
;
int
exitValue = -
1
;
// returned to caller when p is finished
try
{
System.out.println(
"comeing"
);
in = p.getInputStream();
err = p.getErrorStream();
boolean
finished =
false
;
// Set to true when p is finished
while
(!finished) {
try
{
while
(in.available() >
0
) {
Character c =
new
Character((
char
) in.read());
System.out.print(c);
}
while
(err.available() >
0
) {
Character c =
new
Character((
char
) err.read());
System.out.print(c);
}
exitValue = p.exitValue();
finished =
true
;
}
catch
(IllegalThreadStateException e) {
Thread.currentThread().sleep(
500
);
}
}
}
catch
(Exception e) {
System.err.println(
"doWaitFor();: unexpected exception - "
+ e.getMessage());
}
finally
{
try
{
if
(in !=
null
) {
in.close();
}
}
catch
(IOException e) {
System.out.println(e.getMessage());
}
if
(err !=
null
) {
try
{
err.close();
}
catch
(IOException e) {
System.out.println(e.getMessage());
}
}
}
return
exitValue;
}
public
void
deleteFile(String filepath) {
File file =
new
File(filepath);
if
(PATH.equals(filepath)) {
if
(file.delete()) {
System.out.println(
"文件"
+ filepath +
"已删除"
);
}
}
else
{
if
(file.delete()) {
System.out.println(
"文件"
+ filepath +
"已删除 "
);
}
File filedelete2 =
new
File(PATH);
if
(filedelete2.delete()) {
System.out.println(
"文件"
+ PATH +
"已删除"
);
}
}
}
public
String sumTime(
long
ms) {
int
ss =
1000
;
long
mi = ss *
60
;
long
hh = mi *
60
;
long
dd = hh *
24
;
long
day = ms / dd;
long
hour = (ms - day * dd) / hh;
long
minute = (ms - day * dd - hour * hh) / mi;
long
second = (ms - day * dd - hour * hh - minute * mi) / ss;
long
milliSecond = ms - day * dd - hour * hh - minute * mi - second
* ss;
String strDay = day <
10
?
"0"
+ day +
"天"
:
""
+ day +
"天"
;
String strHour = hour <
10
?
"0"
+ hour +
"小时"
:
""
+ hour +
"小时"
;
String strMinute = minute <
10
?
"0"
+ minute +
"分"
:
""
+ minute +
"分"
;
String strSecond = second <
10
?
"0"
+ second +
"秒"
:
""
+ second +
"秒"
;
String strMilliSecond = milliSecond <
10
?
"0"
+ milliSecond :
""
+ milliSecond;
strMilliSecond = milliSecond <
100
?
"0"
+ strMilliSecond +
"毫秒"
:
""
+ strMilliSecond +
" 毫秒"
;
return
strDay +
" "
+ strHour +
":"
+ strMinute +
":"
+ strSecond +
" "
+ strMilliSecond;
}
}
[b]ProcessFlvImg.java[/b]
package
com.ll19.flv;
import
java.io.File;
import
java.util.List;
public
class
ProcessFlvImg {
public
String ffmpegpath =
"D:\\video\\FFmpeg\\bin\\ffmpeg.exe"
;
// ffmpeg.exe的目录
public
boolean
processImg(String path) {
File fi =
new
File(path);
List commend =
new
java.util.ArrayList();
commend.add(ffmpegpath);
commend.add(
"-i"
);
commend.add(path);
commend.add(
"-y"
);
commend.add(
"-f"
);
commend.add(
"image2"
);
commend.add(
"-ss"
);
commend.add(
"38"
);
commend.add(
"-t"
);
commend.add(
"0.001"
);
commend.add(
"-s"
);
commend.add(
"320x240"
);
commend.add(fi.getPath() +
".jpg"
);
try
{
ProcessBuilder builder =
new
ProcessBuilder();
builder.command(commend);
builder.start();
return
true
;
}
catch
(Exception e) {
e.printStackTrace();
return
false
;
}
}
}
|