java视频格式转_java 实现 视频格式转换(转)

直接进入正题吧。

ps:利用ffmpeg 来进行格式转换。

一 建立 java项目。

二 在所建立的java项目中 建立一个ffmpeg文件夹,这个文件夹下放ffmpeg所需的文件。

三 在建立一个 input文件,这个文件是用来存放视频文件,例如xxx.rmvb.

四 在建立一个output文件,这个文件时用来存放你想转换后的视频文件。(例如你想把xxx.rmvb转换成xxx.mp4)

五 之后就是写 java代码。

package com;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.List;

public class ConvertVideo {

private static String inputPath = "";

private static String outputPath = "";

private static String ffmpegPath = "";

public static void main(String args[]) throws IOException {

getPath();

if (!checkfile(inputPath)) {

System.out.println(inputPath + " is not file");

return;

}

if (process()) {

System.out.println("ok");

}

}

public static void getPath() {

// 先获取当前项目路径,在获得源文件、目标文件、转换器的路径

File diretory = new File("");

try {

String currPath = diretory.getAbsolutePath();

inputPath = "E:\\1.mp4";

outputPath = "D:\\vod\\oss\\";

ffmpegPath = "E:\\ffmpeg1\\";

System.out.println(currPath);

}

catch (Exception e) {

System.out.println("getPath出错");

}

}

public static boolean process() {

int type = checkContentType();

boolean status = false;

System.out.println("直接转成mp4格式");

status = processMp4(inputPath);// 直接转成mp4格式

return status;

}

private static int checkContentType() {

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 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 static boolean checkfile(String path) {

File file = new File(path);

if (!file.isFile()) {

return false;

}

return true;

}

// 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.

private static String processAVI(int type) {

List commend = new ArrayList();

commend.add(ffmpegPath + "mencoder");

commend.add(inputPath);

commend.add("-oac");

commend.add("lavc");

commend.add("-lavcopts");

commend.add("acodec=mp3:abitrate=64");

commend.add("-ovc");

commend.add("xvid");

commend.add("-xvidencopts");

commend.add("bitrate=600");

commend.add("-of");

commend.add("mp4");

commend.add("-o");

commend.add(outputPath + "a.AVI");

try {

ProcessBuilder builder = new ProcessBuilder();

Process process = builder.command(commend).redirectErrorStream(true).start();

new PrintStream(process.getInputStream());

new PrintStream(process.getErrorStream());

process.waitFor();

return outputPath + "a.AVI";

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)

private static boolean processFlv(String oldfilepath) {

if (!checkfile(inputPath)) {

System.out.println(oldfilepath + " is not file");

return false;

}

List command = new ArrayList();

command.add(ffmpegPath + "ffmpeg");

command.add("-i");

command.add(oldfilepath);

command.add("-ab");

command.add("56");

command.add("-ar");

command.add("22050");

command.add("-qscale");

command.add("8");

command.add("-r");

command.add("15");

command.add("-s");

command.add("600x500");

command.add(outputPath + "a.flv");

try {

// 方案1

//            Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath

//                    + " -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 "

//                    + outputPath + "a.flv");

// 方案2

Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();

new PrintStream(videoProcess.getErrorStream()).start();

new PrintStream(videoProcess.getInputStream()).start();

videoProcess.waitFor();

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

private static boolean processMp4(String oldfilepath) {

if (!checkfile(inputPath)) {

System.out.println(oldfilepath + " is not file");

return false;

}

List command = new ArrayList();

command.add(ffmpegPath + "ffmpeg");

command.add("-i");

command.add(oldfilepath);

command.add("-c:v");

command.add("libx264");

command.add("-mbd");

command.add("0");

command.add("-c:a");

command.add("aac");

command.add("-strict");

command.add("-2");

command.add("-pix_fmt");

command.add("yuv420p");

command.add("-movflags");

command.add("faststart");

command.add(outputPath + "a.mp4");

try {

// 方案1

//        Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath

//                + " -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 "

//                + outputPath + "a.flv");

// 方案2

Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();

new PrintStream(videoProcess.getErrorStream()).start();

new PrintStream(videoProcess.getInputStream()).start();

videoProcess.waitFor();

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

}

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)

System.out.print((char)_ch);

else break;

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

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

六 运行这段代码就好啦。

ps:FFmpeg 所需的文件链接

https://pan.baidu.com/s/1c2IvaQs

解压后 将 bin 目录下中的ffmpeg.exe 放到ffmpeg目录。

ps: 我的ffmpeg.exe 在E盘.

不过,想真正的学习 视频转换 这方面的还是得去这位大神的博客去学习。

http://blog.csdn.net/leixiaohua1020/article/details/15811977

---------------------

作者:Arog

来源:CSDN

原文:https://blog.csdn.net/qq_32230309/article/details/78875607

版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值