gpt4 book ai didi

java调用ffmpeg实现转换视频

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章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 。

最后此篇关于java调用ffmpeg实现转换视频的文章就讲到这里了,如果你想了解更多关于java调用ffmpeg实现转换视频的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com