Created
February 12, 2021 19:49
-
-
Save illuzor/1207b5ee6ccf290ed856f04161a3c878 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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