Skip to content

Instantly share code, notes, and snippets.

@illuzor
Created February 12, 2021 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save illuzor/1207b5ee6ccf290ed856f04161a3c878 to your computer and use it in GitHub Desktop.
Save illuzor/1207b5ee6ccf290ed856f04161a3c878 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* Copy from https://blog.csdn.net/fjh658/article/details/12869073
*/
public class AmrLength {
public synchronized static long getAmrDuration(File file) throws IOException {
long duration = -1;
int[] packedSize = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw")) {
long length = file.length();
int pos = 6;
int frameCount = 0;
byte[] datas = new byte[1];
while (pos <= length) {
randomAccessFile.seek(pos);
if (randomAccessFile.read(datas, 0, 1) != 1) {
duration = (length - 6) / 650;
break;
}
int packedPos = (datas[0] >> 3) & 0x0F;
pos += packedSize[packedPos] + 1;
frameCount++;
}
duration += frameCount * 20;
}
return duration;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment