Search code examples
ffmpegmp3flashred5

ffmpeg convert any audio format to flash player compatible audio


Right now i am streaming mp3 files using Red5 Server but the problem is flash player is not able to play some of the audio files. So is there any way to convert every audio file uploaded by user to flash player compatible using ffmpeg?


Solution

  • One way to do this is to use ProcessBuilder and call out to ffmpeg; be aware that this will only handle audio file for which your compiled ffmpeg have a codec for.

    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); 
        }
      }
    
    } 
    

    Another way is to use Xuggler, but the project is no longer maintained.

    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();
        }
      }
    
    }