最近由于項(xiàng)目需要把不同格式的視頻轉(zhuǎn)換為ts流,故研究了一下ffmpeg。在網(wǎng)上找了很多資料,主要參考了java+windows+ffmpeg實(shí)現(xiàn)視頻轉(zhuǎn)換功能。
期間也加了幾個(gè)qq群,咨詢(xún)了各大高手,其中在代碼中關(guān)于ffmpeg的命令就是來(lái)自其中一個(gè)qq群里面的大神。
下載相關(guān)文件
ffmpeg地址,我下載是windows 64位static版本。
xuggler下載地址
下面的代碼我上傳到了github,需要的可以下載下來(lái)看看。
步驟:
1.研究java如何調(diào)用外部程序
2.研究ffmpeg轉(zhuǎn)換視頻格式的命令
3.利用xuggle獲取ffmpeg解析的ts流的時(shí)長(zhǎng)、分辨率以及文件大小。
下面直接上代碼:
1.ffmpeg轉(zhuǎn)換實(shí)現(xiàn)
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
|
package vedio.ffmpeg; import java.io.file; import java.util.arraylist; import java.util.list; public class ffmpegutil { public static boolean ffmpeg(stringffmpegpath, string inputpath, string outputpath) throwsffmpegexception{ if (!checkfile(inputpath)) { throw newffmpegexception( "文件格式不合法" ); } int type =checkcontenttype(inputpath); list command = getffmpegcommand(type,ffmpegpath, inputpath, outputpath); if ( null != command &&command.size() > 0 ) { return process(command); } return false ; } private static int checkcontenttype(stringinputpath) { string type =inputpath.substring(inputpath.lastindexof( "." ) + 1 ,inputpath.length()).tolowercase(); //ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) if (type.equals( "avi" )) { return 1 ; } else if (type.equals( "mpg" )){ return 1 ; } else if (type.equals( "wmv" )){ return 1 ; } else if (type.equals( "3gp" )){ return 1 ; } else if (type.equals( "mov" )){ return 1 ; } else if (type.equals( "mp4" )){ return 1 ; } else if (type.equals( "mkv" )){ return 1 ; } else if (type.equals( "asf" )){ return 0 ; } else if (type.equals( "flv" )){ return 0 ; } else if (type.equals( "rm" )){ return 0 ; } else if (type.equals( "rmvb" )){ return 1 ; } return 9 ; } private static boolean checkfile(stringpath) { file file = new file(path); if (!file.isfile()) { return false ; } return true ; } private static boolean process(listcommand) throws ffmpegexception{ try { if ( null == command || command.size() == 0 ) return false ; process videoprocess = newprocessbuilder(command).redirecterrorstream( true ).start(); newprintstream(videoprocess.geterrorstream()).start(); newprintstream(videoprocess.getinputstream()).start(); int exitcode =videoprocess.waitfor(); if (exitcode == 1 ) { return false ; } return true ; } catch (exception e) { throw new ffmpegexception( "file uploadfailed" ,e); } } private static list getffmpegcommand(inttype, string ffmpegpath, string oldfilepath, string outputpath) throws ffmpegexception { list command = newarraylist(); if (type == 1 ) { command.add(ffmpegpath + "\\ffmpeg" ); command.add( "-i" ); command.add(oldfilepath); command.add( "-c:v" ); command.add( "libx264" ); command.add( "-x264opts" ); command.add( "force-cfr=1" ); command.add( "-c:a" ); command.add( "mp2" ); command.add( "-b:a" ); command.add( "256k" ); command.add( "-vsync" ); command.add( "cfr" ); command.add( "-f" ); command.add( "mpegts" ); command.add(outputpath); } else if (type== 0 ){ command.add(ffmpegpath + "\\ffmpeg" ); command.add( "-i" ); command.add(oldfilepath); command.add( "-c:v" ); command.add( "libx264" ); command.add( "-x264opts" ); command.add( "force-cfr=1" ); command.add( "-vsync" ); command.add( "cfr" ); command.add( "-vf" ); command.add( "idet,yadif=deint=interlaced" ); command.add( "-filter_complex" ); command.add( "aresample=async=1000" ); command.add( "-c:a" ); command.add( "libmp3lame" ); command.add( "-b:a" ); command.add( "192k" ); command.add( "-pix_fmt" ); command.add( "yuv420p" ); command.add( "-f" ); command.add( "mpegts" ); command.add(outputpath); } else { throw newffmpegexception( "不支持當(dāng)前上傳的文件格式" ); } return command; } } class printstream extends thread{ java.io.inputstream __is = null ; public printstream(java.io.inputstream is){ __is = is; } public void run() { try { while ( this != null ) { int _ch = __is.read(); if (_ch == - 1 ) { break ; } else { system.out.print(( char ) _ch); } } } catch (exception e) { e.printstacktrace(); } } } |
2.調(diào)用測(cè)試類(lèi)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package vedio.ffmpeg; public class convertvedio { public static void convertvedio(stringinputpath){ string ffmpegpath =getffmpegpath(); string outputpath =getoutputpath(inputpath); try { ffmpegutil.ffmpeg(ffmpegpath, inputpath,outputpath); } catch (ffmpegexception e) { e.printstacktrace(); } } private static string getffmpegpath(){ return "ffmpeg" ; } private static string getoutputpath(stringinputpath) { return inputpath.substring( 0 ,inputpath.lastindexof( "." )).tolowercase() + ".ts" ; } } |
3.自定義的異常類(lèi)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package vedio.ffmpeg; public class ffmpegexception extendsexception { private static final long serialversionuid= 1l; public ffmpegexception() { super (); } public ffmpegexception(string message){ super (message); } public ffmpegexception(throwable cause){ super (cause); } public ffmpegexception(string message,throwable cause) { super (message, cause); } } |
4.獲取ts流的時(shí)長(zhǎng)、大小以及分辨率(用到了xuggle,需要下載對(duì)應(yīng)jar包)
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
|
importcom.xuggle.xuggler.icodec; importcom.xuggle.xuggler.icontainer; importcom.xuggle.xuggler.istream; importcom.xuggle.xuggler.istreamcoder; */ public static void getvedioinfo(string filename){ // first we create a xuggler containerobject icontainer container =icontainer.make(); // we attempt to open up thecontainer int result = container.open(filename,icontainer.type.read, null ); // check if the operation wassuccessful if (result< 0 ) return ; // query how many streams the call to openfound int numstreams =container.getnumstreams(); // query for the total duration long duration =container.getduration(); // query for the file size long filesize =container.getfilesize(); long secondduration =duration/ 1000000 ; system.out.println( "時(shí)長(zhǎng):" +secondduration+ "秒" ); system.out.println( "文件大小:" +filesize+ "m" ); for ( int i= 0 ; i istreamstream = container.getstream(i); istreamcoder coder = stream.getstreamcoder(); if (coder.getcodectype() == icodec.type.codec_type_video){ system.out.println( "視頻寬度:" +coder.getwidth()); system.out.println( "視頻高度:" +coder.getheight()); } } } |
以上就是在開(kāi)發(fā)過(guò)程中做的全部,希望大家多多學(xué)習(xí),交流!
原文鏈接:https://blog.csdn.net/zhengdesheng19930211/article/details/64443620