android-opus-codec
android-opus-codec copied to clipboard
transcode m4a to opus
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();
}
}
I think the issue lies in FRAME option Or writing the buffer to outputstream
What I'm aiming for:
Convert any audio to opus format at 24kbps bitrate
@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.
Can you add some helper methods in the library eg
encodeToFile(InputStream input,File output,ProgressListener listener)
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
@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.
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