服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - Java视频格式转化的实现方法

Java视频格式转化的实现方法

2021-04-08 14:36liuyazhuang JAVA教程

这篇文章主要为大家详细介绍了Java视频格式转化的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java视频格式转化的具体代码,供大家参考,具体内容如下

核心是利用ffmpeg进行视频转换,我们自己并不写转换视频的代码,只是调用ffmpeg,它会帮我们完成视频的转换。ffmpeg支持的类型有:asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等,这些类型,可以利用ffmpeg进行直接转换。ffmpeg不支持的类型有:wmv9,rm,rmvb等,这些类型需要先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式。

废话不大多说了,首先要把相关的库和要转化的视频准备好,如下图

 Java视频格式转化的实现方法

下面就是代码部分了   

?
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
package com.sino.test;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
/**
 * java实现视频格式的转化
 * @author liuyazhuang
 *
 */
public class ChangeVideo {
  public static void main(String[] args) {
    ChangeVideo.convert("d:\\myeclipse\\aa.avi", "d:\\myeclipse\\bb.mp4");
  }
 
  /**
   * @param inputFile:需要转换的视频
   * @param outputFile:转换后的视频w
   * @return
   */
  public static boolean convert(String inputFile, String outputFile) {
    if (!checkfile(inputFile)) {
      System.out.println(inputFile + " is nokt file");
      return false;
    }
    if (process(inputFile, outputFile)) {
      System.out.println("ok");
      return true;
    }
    return false;
  }
 
  // 检查文件是否存在
  private static boolean checkfile(String path) {
    File file = new File(path);
    if (!file.isFile()) {
      return false;
    }
    return true;
  }
 
  /**
   * @param inputFile
   * @param outputFile
   * @return
   * 转换视频文件
   */
  private static boolean process(String inputFile, String outputFile) {
    int type = checkContentType(inputFile);
    boolean status = false;
    if (type == 0) {
      status = processFLV(inputFile, outputFile);// 直接将文件转为flv文件
    } else if (type == 1) {
      String avifilepath = processAVI(type, inputFile);
      if (avifilepath == null)
        return false;// avi文件没有得到
      status = processFLV(avifilepath, outputFile);// 将avi转为flv
    }
    return status;
  }
 
  private static int checkContentType(String inputFile) {
    String type = inputFile.substring(inputFile.lastIndexOf(".") + 1,
        inputFile.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;
  }
  // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)直接转换为目标视频
  private static boolean processFLV(String inputFile, String outputFile) {
    if (!checkfile(inputFile)) {
      System.out.println(inputFile + " is not file");
      return false;
    }
    List<String> commend = new ArrayList<String>();
     
    commend.add(Constants.ffmpegPath);
    commend.add("-i");
    commend.add(inputFile);
    commend.add("-ab");
    commend.add("128");
    commend.add("-acodec");
    commend.add("libmp3lame");
    commend.add("-ac");
    commend.add("1");
    commend.add("-ar");
    commend.add("22050");
    commend.add("-r");
    commend.add("29.97");
    //高品质 
    commend.add("-qscale");
    commend.add("6");
    //低品质
//   commend.add("-b");
//   commend.add("512");
    commend.add("-y");
     
    commend.add(outputFile);
    StringBuffer test = new StringBuffer();
    for (int i = 0; i < commend.size(); i++) {
      test.append(commend.get(i) + " ");
    }
 
    System.out.println(test);
 
    try {
      ProcessBuilder builder = new ProcessBuilder();
      builder.command(commend);
      builder.start();
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
  // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
  private static String processAVI(int type, String inputFile) {
    File file = new File(Constants.avifilepath);
    if (file.exists())
      file.delete();
    List<String> commend = new ArrayList<String>();
    commend.add(Constants.mencoderPath);
    commend.add(inputFile);
    commend.add("-oac");
    commend.add("mp3lame");
    commend.add("-lameopts");
    commend.add("preset=64");
    commend.add("-ovc");
    commend.add("xvid");
    commend.add("-xvidencopts");
    commend.add("bitrate=600");
    commend.add("-of");
    commend.add("avi");
    commend.add("-o");
    commend.add(Constants.avifilepath);
    StringBuffer test = new StringBuffer();
    for (int i = 0; i < commend.size(); i++) {
      test.append(commend.get(i) + " ");
    }
 
    System.out.println(test);
    try {
      ProcessBuilder builder = new ProcessBuilder();
      builder.command(commend);
      Process p = builder.start();
 
      final InputStream is1 = p.getInputStream();
      final InputStream is2 = p.getErrorStream();
      new Thread() {
        public void run() {
          BufferedReader br = new BufferedReader(
              new InputStreamReader(is1));
          try {
            String lineB = null;
            while ((lineB = br.readLine()) != null) {
              if (lineB != null)
                System.out.println(lineB);
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }.start();
      new Thread() {
        public void run() {
          BufferedReader br2 = new BufferedReader(
              new InputStreamReader(is2));
          try {
            String lineC = null;
            while ((lineC = br2.readLine()) != null) {
              if (lineC != null)
                System.out.println(lineC);
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }.start();
 
      // 等Mencoder进程转换结束,再调用ffmepg进程
      p.waitFor();
      System.out.println("who cares");
      return Constants.avifilepath;
    } catch (Exception e) {
      System.err.println(e);
      return null;
    }
  }
}

 类ChangeVideo主要进行视频格式的转化

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.sino.test;
 
/**
 * 常量类,主要设置可执行程序和动态链接库以及转化过程中生成的临时视频文件的位置
 * @author liuyazhuang
 *
 */
public class Constants {
  //ffmpeg存放的路径
  public static final String ffmpegPath = "d:\\myeclipse\\ffmpeg.exe";
  //mencoder存放的路径
  public static final String mencoderPath = "d:\\myeclipse\\mencoder.exe";
  //通过mencoder转换成的avi存放路径
  public static final String avifilepath = "d:\\myeclipse\\temp.avi";
}

常量类Constants ,主要设置可执行程序和动态链接库以及转化过程中生成的临时视频文件的位置。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/l1028386804/article/details/44889781

延伸 · 阅读

精彩推荐