tags:

views:

72

answers:

2

Hi all, I am tyring to merge an .mov file with a .wav file using java media framework, thus I need to know their duration. How can I do this? Any ideas would be appreciated..

+2  A: 

I am not familiar with java media framework, but probably the following will help:

1) Theoretically you can get duration of a wav file from the "fact" chunk. But I doubt, JMF give direct access to the chunk. Moreover the chunk can contain an incorrect value.

2) You can calculate duration, knowing audio data size (in bytes) and sample rate (or bitrate).

VitalyVal
Could you please give a concrete example ?
I'm afraid, I am not sure what you mean by the "concrete example". As I said I am not familiar with JMF. Moreover, as for point 1), you simply need to read RIFF-WAV file specifications. As for point 2: duration(in sec.)=data_size/block_align/samplerate. What I wanted to tell - there are only two possible common methods to determine duration of wav file without decompressing.
VitalyVal
+1  A: 

you can learn the duration of sound files using this way(that is VitalyVal's second way):

  import java.net.URL;

        import javax.sound.sampled.AudioFormat;
        import javax.sound.sampled.AudioInputStream;
        import javax.sound.sampled.AudioSystem;
        import javax.sound.sampled.Clip;
        import javax.sound.sampled.DataLine;

        public class SoundUtils {
            public static double getLength(String path) throws Exception {
                AudioInputStream stream;
                stream = AudioSystem.getAudioInputStream(new URL(path));
                AudioFormat format = stream.getFormat();
                if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
                    format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format
                            .getSampleRate(), format.getSampleSizeInBits() * 2, format
                            .getChannels(), format.getFrameSize() * 2, format
                            .getFrameRate(), true); // big endian
                    stream = AudioSystem.getAudioInputStream(format, stream);
                }
                DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
                        ((int) stream.getFrameLength() * format.getFrameSize()));
                Clip clip = (Clip) AudioSystem.getLine(info);
                clip.close();
                return clip.getBufferSize()
                        / (clip.getFormat().getFrameSize() * clip.getFormat()
                                .getFrameRate());
            }

            public static void main(String[] args) {
                try {

                    System.out
                            .println(getLength("..."));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
ibrahimyilmaz