ffmpeg将任何音频格式转换为兼容Flash播放器的音频

时间:2014-07-07 03:20:15

标签: ffmpeg mp3 flash red5

现在我正在使用Red5 Server传输mp3文件,但问题是Flash播放器无法播放某些音频文件。 那么有没有办法将用户上传的每个音频文件转换为使用ffmpeg兼容的flash播放器?

1 个答案:

答案 0 :(得分:0)

这样做的一种方法是使用ProcessBuilder并呼叫ffmpeg;请注意,这只会处理已编译的ffmpeg具有编解码器的音频文件。

import java.io.File;
import java.io.IOException;

public class SimpleTranscoder {

  public static void transcode(File inputFile) {
    ProcessBuilder pb = new ProcessBuilder("C:\\ffmpeg\\ffmpeg.exe", "-i", inputFile.getAbsolutePath(), "-acodec", "libmp3lame", "-asamplerate", "44100", "-ab", "32k", "-vn", (inputFile.getParent() + '/' + inputFile.getName() + ".mp3")); 
    pb.redirectErrorStream(true); 
    Process p = pb.start(); 
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); 
    String line = null; 
    while ((line=br.readLine()) != null) { 
      System.out.println(line); 
    }
  }

} 

另一种方法是使用Xuggler,但不再维护该项目。

import com.xuggle.xuggler.Converter;
import java.io.File;
import java.io.IOException;

public class SimpleTranscoder {

  public static void transcode(File inputFile) {
    Converter converter = new Converter();
    // pass options normally used on the command line 
    String[] arguments = {
          inputFile.getAbsolutePath(),
          "-acodec", "libmp3lame",
          "-asamplerate", "44100",
          "-ab", "32k",
          "-vn",
          inputFile.getParent() + '/' + inputFile.getName() + ".mp3"
          }
    try {
      // run the transcoder with the options we provided.
      converter.run(converter.parseOptions(converter.defineOptions(), arguments));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}
相关问题