激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻

java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻

2021-06-22 13:17zhengdesheng19930211 Java教程

這篇文章主要為大家詳細(xì)介紹了java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近由于項(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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人激情视频网 | 日本中文字幕网址 | 久久国产精品电影 | 国产乱一区二区三区视频 | 中国杭州少妇xxxx做受 | 欧美成人免费一区二区三区 | 精品成人国产在线观看男人呻吟 | 中文字幕精品在线视频 | 欧美精品国产综合久久 | 黄色毛片视频在线观看 | 国产成人精品网站 | 欧美激情综合在线 | 国产精品午夜在线观看 | 亚洲精品永久视频 | 国产精品久久久久久久午夜片 | 青青草最新网址 | 日韩剧情片 | 日本aaaa片毛片免费观蜜桃 | 成人免费观看49www在线观看 | 911色_911色sss主站色播 | 色婷婷tv | 性高湖久久久久久久久aaaaa | 国产91精品久久久久久久 | 九九热色 | 青青久热 | 欧美中文在线 | 激情久久精品 | 欧美成人免费小视频 | 神秘电影91 | 久草在线视频看看 | 欧美国产日韩在线观看成人 | 国产精品观看在线亚洲人成网 | 369看片你懂的小视频在线观看 | 性爱视频在线免费 | 91久久久久久久 | 免费黄色小视频网站 | 91超在线| 黄色的视频免费看 | 日韩精品hd | 午夜国产福利 | 国产亚洲高清在线精品不卡 |