国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java調用ffmpeg實現轉換視頻

java調用ffmpeg實現轉換視頻

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

這篇文章主要為大家詳細介紹了java調用ffmpeg實現轉換視頻功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近由于項目需要把不同格式的視頻轉換為ts流,故研究了一下ffmpeg。在網上找了很多資料,主要參考了java+windows+ffmpeg實現視頻轉換功能。

期間也加了幾個qq群,咨詢了各大高手,其中在代碼中關于ffmpeg的命令就是來自其中一個qq群里面的大神。

下載相關文件

ffmpeg地址,我下載是windows 64位static版本。

xuggler下載地址

下面的代碼我上傳到了github,需要的可以下載下來看看。

步驟:

1.研究java如何調用外部程序
2.研究ffmpeg轉換視頻格式的命令
3.利用xuggle獲取ffmpeg解析的ts流的時長、分辨率以及文件大小。

下面直接上代碼:

1.ffmpeg轉換實現

?
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("不支持當前上傳的文件格式");
}
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.調用測試類

?
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.自定義的異常類

?
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流的時長、大小以及分辨率(用到了xuggle,需要下載對應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("時長:"+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());
    }
   }
 
 }

以上就是在開發過程中做的全部,希望大家多多學習,交流!

原文鏈接:https://blog.csdn.net/zhengdesheng19930211/article/details/64443620

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: www.国产 | 中文字幕av第一页 | 欧美一级全黄 | 日本一区高清 | 国产亲子乱弄免费视频 | 日韩精品久久久 | 欧美日韩国产一区二区三区 | 日韩h视频 | 精品国产青草久久久久福利 | 在线91| 一级黄毛片 | 91电影在线观看 | 美女视频黄a| 中文字幕第9页 | 亚洲视频一区二区 | 激情网五月天 | 久久久久久久99 | 在线观看一区二区三区四区 | 亚洲va欧美va人人爽成人影院 | av在线网址观看 | 亚洲精品中文字幕在线观看 | 国产深夜视频在线观看 | 久久国产精品99国产精 | 国产欧美精品一区二区三区 | 国产精品不卡一区二区三区 | 日本久久精品视频 | 日韩欧美在线观看 | 久草在线 | 久久久成人精品 | 亚洲精品国产第一综合99久久 | 色综合一区二区三区 | 久久久免费国产 | 色站综合 | 97成人在线免费视频 | 久久九 | 羞羞视频在线播放 | 欧美中文字幕在线 | 欧美专区在线观看 | 久久成人av | 日韩精品久久久 | 亚洲激情在线观看 |