bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.41k stars 1.57k forks source link

Assuming the audio is in stereo, how to extract the left channel and right channel ? #2129

Closed Mhuang77 closed 7 months ago

Mhuang77 commented 7 months ago
import org.bytedeco.javacv.*;

public class TestMain {
    public static void main(String[] args) {
       // Replace with your audio file path
        String audioFilePath = "test.mp3"; 
        try (FrameGrabber grabber = new FFmpegFrameGrabber(audioFilePath)) {
            grabber.start();
            Frame frame;
            while ((frame = grabber.grab()) != null) {
                // Assuming the audio is in stereo, extract the left channel and right channel
                // TODO extract the left channel and right channel <------ ?
            }
            grabber.stop();
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        }
    }
}

I searched the Frame class methods but did not find a way can extract the left channel and right channel.

saudet commented 7 months ago

There's some sample code for that here thanks to @steeveen: https://github.com/bytedeco/javacv/blob/master/samples/AudioSplitMergeHelper.java