theeasiestway / android-opus-codec

Implementation of Opus encoder and decoder in C++ for android using JNI
103 stars 31 forks source link

transcode m4a to opus #12

Open omkar-tenkale opened 3 years ago

omkar-tenkale commented 3 years ago

i've written this code but the output file fails to play


            Constants.SampleRate SAMPLE_RATE =  Constants.SampleRate.Companion._48000();
            Constants.Channels CHANNELS = Constants.Channels.Companion.mono();
            Constants.Application APPLICATION = Constants.Application.Companion.voip();
            Constants.FrameSize FRAME_SIZE = Constants.FrameSize.Companion._320();

try {
                    try (OutputStream output = new FileOutputStream(dest)) {
                        codec = new Opus();
                        codec.encoderInit(SAMPLE_RATE, CHANNELS, APPLICATION);
                        byte[] buffer = new byte[320];
                        int read;
                        while ((read = target.read(buffer)) != -1) {
                            if (isCancelled()) {
                                break;
                            }
                            byte[] encodedBuffer=  codec.encode(buffer, FRAME_SIZE);
                            int lenEncodedBytes = encodedBuffer.length;
                            if (lenEncodedBytes > 0)
                            {
                                output.write(lenEncodedBytes);
                                output.write(encodedBuffer,0,lenEncodedBytes);
                                Log.e("Encoder","Encode Bytes "+lenEncodedBytes);
                            }
                            currentProgress += lenEncodedBytes;
                            notifyProgress();
                        }
                        publishProgress(100);
                        output.flush();
                        success = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        target.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
omkar-tenkale commented 3 years ago

I think the issue lies in FRAME option Or writing the buffer to outputstream

omkar-tenkale commented 3 years ago

What I'm aiming for:

Convert any audio to opus format at 24kbps bitrate

theeasiestway commented 3 years ago

@omkar-tenkale I'm not sure but I think the problem is that the output file doesn't have the needed headers that a player can read and understand that the file is opus audio so that's why it failed to play.

omkar-tenkale commented 3 years ago

Can you add some helper methods in the library eg

encodeToFile(InputStream input,File output,ProgressListener listener)

omkar-tenkale commented 3 years ago

Can you please clarify use of FRAME_SIZE option and if it affects buffer size Also which parameters affect final file size and how eg Bitrate directly proportional to output file size

theeasiestway commented 3 years ago

@omkar-tenkale sorry but currently I'm not planning to add methods for saving the output to file. As for codec parameters - since this project it's just a wrapper around C implementation of opus you can check the official documentation here for encoder and here for decoder.

omkar-tenkale commented 3 years ago

sorry but currently I'm not planning to add methods for saving the output to file.

Not a good news,hope you'll add it later

I'll be using this lib for converting audio to opus then, https://github.com/tanersener/mobile-ffmpeg Only drawback is its >30mb while this library is <1mb