3

I am trying to convert mp3 to wav and the code is here:

String mp3 = "F:\\work\\pic2talk38512.mp3";
String wav = "F:\\work\\pic2talk38512.wav";
TranscodeAudioAndVideo.transcodeTest(mp3, wav);

public static void transcode(String sourceUrl, String destinationUrl) {
    IMediaReader reader = ToolFactory.makeReader(sourceUrl);
    reader.addListener(ToolFactory.makeWriter(destinationUrl, reader));

    while (reader.readPacket() == null)
            do {
            } while (false);
}

I got this exception:

java.lang.UnsupportedOperationException: could not guess codec
at com.xuggle.xuggler.IContainerFormat.establishOutputCodecId(IContainerFormat.java:454)
at com.xuggle.xuggler.IContainerFormat.establishOutputCodecId(IContainerFormat.java:327)
at com.xuggle.xuggler.IContainerFormat.establishOutputCodecId(IContainerFormat.java:300)
at com.xuggle.mediatool.MediaWriter.addStreamFromContainer(MediaWriter.java:1134)
at com.xuggle.mediatool.MediaWriter.getStream(MediaWriter.java:1039)
at com.xuggle.mediatool.MediaWriter.encodeVideo(MediaWriter.java:742)
at com.xuggle.mediatool.MediaWriter.encodeVideo(MediaWriter.java:783)
at com.xuggle.mediatool.MediaWriter.onVideoPicture(MediaWriter.java:1434)
at com.xuggle.mediatool.AMediaToolMixin.onVideoPicture(AMediaToolMixin.java:166)
at com.xuggle.mediatool.MediaReader.dispatchVideoPicture(MediaReader.java:610)
at com.xuggle.mediatool.MediaReader.decodeVideo(MediaReader.java:519)
at com.xuggle.mediatool.MediaReader.readPacket(MediaReader.java:475)
at com.renren.intl.soundsns.pictalk.utils.TranscodeAudioAndVideo.transcodeTest(TranscodeAudioAndVideo.java:75)
at com.renren.intl.soundsns.pictalk.utils.TranscodeAudioAndVideoTest.convert(TranscodeAudioAndVideoTest.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I know that xuggle can figure out the conversion by the extension.But why this time is failed?

thanks

edit:

previously I can convert mp3 which params(MPEG 22.1KHZ 32kbps) to wav, but If I use the mp3(MPEG 44.1KHZ 62kbps) I got this exception?

How this happened?

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
Felix
  • 1,253
  • 7
  • 22
  • 41

2 Answers2

4

In this ,basically xuggle is complaining about your codec.I think you need to try this code

String mp3 = "F:\\work\\pic2talk38512.mp3";

String wav = "F:\\work\\pic2talk38512.wav";    
IMediaReader mediaReader = ToolFactory.makeReader(mp3);
            IMediaWriter mediaWriter = ToolFactory.makeWriter(wav, mediaReader);
            mediaReader.addListener(mediaWriter);
            // IMediaViewer mediaViewer = ToolFactory.makeViewer(true);
            // mediaReader.addListener(mediaViewer);
            while (mediaReader.readPacket() == null);

I hope you are using xuggle-xuggler-5.4.jar

Freak
  • 6,786
  • 5
  • 36
  • 54
1

Update:

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.IMediaViewer;
import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.ToolFactory;
public class TranscodingExample {
    private static final String inputFilename = "c:/myvideo.mp4";
    private static final String outputFilename = "c:/myvideo.flv";
    public static void main(String[] args) {// create a media reader
        IMediaReader mediaReader =
               ToolFactory.makeReader(inputFilename);
        // create a media writer
        IMediaWriter mediaWriter =
               ToolFactory.makeWriter(outputFilename, mediaReader);
        // add a writer to the reader, to create the output file
        mediaReader.addListener(mediaWriter);
        // create a media viewer with stats enabled
        IMediaViewer mediaViewer = ToolFactory.makeViewer(true);
        // add a viewer to the reader, to see the decoded media
        mediaReader.addListener(mediaViewer);
        // read and decode packets from the source file and
        // and dispatch decoded audio and video to the writer
        while (mediaReader.readPacket() == null) ;
    } 
}  

This is the code taken from Java Code Geeks. Try changing the path to your mp3 files and see if that works for you

Proof of code working

enter image description here

Trnscoding mp3 @ 64kbps to wav

enter image description here

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • I have used Xuggler previously. Let me have a look. In the mean time, try changing the back slashes to forward slashes :) – An SO User Apr 11 '13 at 09:41
  • this is not the slashers issue.OP is using slashes correctly. – Freak Apr 11 '13 at 09:47
  • previously I can convert mp3 which params(MPEG 22.1KHZ 32kbps) to wav using demo code, but If I use the mp3(MPEG 44.1KHZ 62kbps) I got this exception. – Felix Apr 11 '13 at 09:52
  • I can convert mp3 which is downloaded from google voice to wav, but I can not convert my mp3 to wav.How this happened... – Felix Apr 11 '13 at 09:54
  • 2
    @LittleChild dont be so excited.It can happen because of the variations of codec.do concentrate what @felix is saying `I can convert mp3 which params(MPEG 22.1KHZ 32kbps) to wav using demo code, but If I use the mp3(MPEG 44.1KHZ 62kbps) I got this exception.` You need to read this comment again – Despicable Apr 11 '13 at 09:58
  • 1
    @LittleChild Can I send my mp3 to you by email? If you don`t want to public your email here, my email is guofuchunlh@gmail.com and would you send me an email and I send mp3 to you? thanks ): I think my mp3 file is wrong but I can play it... – Felix Apr 11 '13 at 10:24