Android之音频和视频的提取
权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
private boolean checkPermission() {
return ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 101 && grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showShortToast("权限申请成功");
} else {
showShortToast("权限申请拒绝");
}
}
}
将视频音频分离开
将视频和音频分离开
private void splitMediaToVideoAndRadio(){
MediaExtractor extractor = new MediaExtractor();
try {
extractor.setDataSource(getFileDirPath()+File.separator + "source_demo.mp4");
} catch (IOException e) {
e.printStackTrace();
showShortToast("视频资源路径出错");
}
int trackCount = extractor.getTrackCount();
int videoTrackIndex = 0;
MediaFormat videoMediaFormat = null;
int audioTrackIndex = 0;
MediaFormat audioMediaFormat = null;
for (int i = 0; i < trackCount; i++) {
MediaFormat itemMediaFormat = extractor.getTrackFormat(i);
String itemMime = itemMediaFormat.getString(MediaFormat.KEY_MIME);
if (itemMime.startsWith("video")) {
videoTrackIndex = i;
videoMediaFormat = itemMediaFormat;
Log.d(TAG,"video format start --- ");
Log.d(TAG,"video format --> \r\n"+itemMediaFormat.toString());
Log.d(TAG,"video format end --- ");
}else if (itemMime.startsWith("audio")) {
audioTrackIndex = i;
audioMediaFormat = itemMediaFormat;
Log.d(TAG,"audio format start --- ");
Log.d(TAG,"audio format --> \r\n"+itemMediaFormat.toString());
Log.d(TAG,"audio format end --- ");
}
}
File outVideoFile = new File(getFileOutPath() + File.separator + "video.h264");
File outRadioFile = new File(getFileOutPath() + File.separator + "audio.acc");
if(outVideoFile.exists()){
outVideoFile.delete();
}
if(outRadioFile.exists()){
outRadioFile.delete();
}
try{
FileOutputStream videoOutputStream = new FileOutputStream(outVideoFile);
FileOutputStream radioOutputStream = new FileOutputStream(outRadioFile);
int maxVideoBufferCount = videoMediaFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
ByteBuffer videoByteBuffer = ByteBuffer.allocate(maxVideoBufferCount);
extractor.selectTrack(videoTrackIndex);
int length;
while ((length = extractor.readSampleData(videoByteBuffer,0)) != -1){
byte [] bytes = new byte[length];
videoByteBuffer.get(bytes);
videoOutputStream.write(bytes);
videoByteBuffer.clear();
extractor.advance();
}
videoOutputStream.flush();
videoOutputStream.close();
extractor.unselectTrack(videoTrackIndex);
int maxAudioBufferCount = audioMediaFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
ByteBuffer audioByteBuffer = ByteBuffer.allocate(maxAudioBufferCount);
extractor.selectTrack(audioTrackIndex);
while ((length = extractor.readSampleData(audioByteBuffer, 0)) != -1) {
byte[] bytes = new byte[length];
audioByteBuffer.get(bytes);
byte[] adtsData = new byte[length + 7];
addADTStoPacket(adtsData, length+7);
System.arraycopy(bytes, 0, adtsData, 7, length);
radioOutputStream.write(bytes);
audioByteBuffer.clear();
extractor.advance();
}
radioOutputStream.flush();
radioOutputStream.close();
showShortToast("分离音频视频成功");
}catch (Exception e){
e.printStackTrace();
showShortToast("分离音频视频失败");
}
}
private static void addADTStoPacket(byte[] packet, int packetLen) {
int profile = 2;
int frequencyIndex = 0x04;
int channelConfiguration = 2;
packet[0] = (byte) 0xFF;
packet[1] = (byte) 0xF9;
packet[2] = (byte) (((profile - 1) << 6) + (frequencyIndex << 2) + (channelConfiguration >> 2));
packet[3] = (byte) (((channelConfiguration & 3) << 6) + (packetLen >> 11));
packet[4] = (byte) ((packetLen & 0x7FF) >> 3);
packet[5] = (byte) (((packetLen & 7) << 5) + 0x1F);
packet[6] = (byte) 0xFC;
}
将视频分离成无声视频和音频
视频
private void generateMediaVideo(){
MediaExtractor extractor = new MediaExtractor();
try {
extractor.setDataSource(getFileDirPath()+File.separator + "source_demo.mp4");
} catch (IOException e) {
e.printStackTrace();
showShortToast("视频资源路径出错");
}
int trackCount = extractor.getTrackCount();
int videoTrackIndex = 0;
MediaFormat videoMediaFormat = null;
for (int i = 0; i < trackCount; i++) {
MediaFormat itemMediaFormat = extractor.getTrackFormat(i);
String itemMime = itemMediaFormat.getString(MediaFormat.KEY_MIME);
if (itemMime.startsWith("video")) {
videoTrackIndex = i;
videoMediaFormat = itemMediaFormat;
Log.d(TAG,"video format start --- ");
Log.d(TAG,"video format --> \r\n"+itemMediaFormat.toString());
Log.d(TAG,"video format end --- ");
}
}
File outVideoFile = new File(getFileOutPath() + File.separator + "out_video.mp4");
if(outVideoFile.exists()){
outVideoFile.delete();
}
try{
MediaMuxer mediaMuxer = new MediaMuxer(outVideoFile.getAbsolutePath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
int mVideoTrackIndex = mediaMuxer.addTrack(videoMediaFormat);
mediaMuxer.start();
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
info.presentationTimeUs = 0;
extractor.selectTrack(videoTrackIndex);
int videoFlag = extractor.getSampleFlags();
long videoFrameRate = 1000 * 1000 /(videoMediaFormat.getInteger(MediaFormat.KEY_FRAME_RATE));
int length;
ByteBuffer buffer = ByteBuffer.allocate(100 * 1024);
while ((length = extractor.readSampleData(buffer,0)) != -1){
info.offset = 0;
info.size = length;
info.flags = videoFlag;
info.presentationTimeUs += videoFrameRate;
mediaMuxer.writeSampleData(mVideoTrackIndex, buffer, info);
extractor.advance();
}
extractor.release();
mediaMuxer.stop();
mediaMuxer.release();
showShortToast("生成视频文件成功");
}catch (Exception e){
e.printStackTrace();
showShortToast("视频生成失败");
}
}
音频
private void generateMediaRadio(){
MediaExtractor extractor = new MediaExtractor();
try {
extractor.setDataSource(getFileDirPath()+File.separator + "source_demo.mp4");
} catch (IOException e) {
e.printStackTrace();
showShortToast("视频资源路径出错");
}
int trackCount = extractor.getTrackCount();
int radioTrackIndex = 0;
MediaFormat radioMediaFormat = null;
long radioFrameRate = 0;
for (int i = 0; i < trackCount; i++) {
MediaFormat itemMediaFormat = extractor.getTrackFormat(i);
String itemMime = itemMediaFormat.getString(MediaFormat.KEY_MIME);
if (itemMime.startsWith("audio")) {
radioTrackIndex = i;
radioMediaFormat = itemMediaFormat;
Log.d(TAG,"audio format start --- ");
Log.d(TAG,"audio format --> \r\n"+itemMediaFormat.toString());
Log.d(TAG,"audio format end --- ");
extractor.selectTrack(i);
long first_sampletime = extractor.getSampleTime();
extractor.advance();
long second_sampletime = extractor.getSampleTime();
radioFrameRate = Math.abs(second_sampletime - first_sampletime);
extractor.unselectTrack(i);
}
}
File outVideoFile = new File(getFileOutPath() + File.separator + "out_audio.mp4");
if(outVideoFile.exists()){
outVideoFile.delete();
}
try{
MediaMuxer mediaMuxer = new MediaMuxer(outVideoFile.getAbsolutePath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
int mVideoTrackIndex = mediaMuxer.addTrack(radioMediaFormat);
mediaMuxer.start();
MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
info.presentationTimeUs = 0;
extractor.selectTrack(radioTrackIndex);
int videoFlag = extractor.getSampleFlags();
int length;
ByteBuffer buffer = ByteBuffer.allocate(100 * 1024);
while ((length = extractor.readSampleData(buffer,0)) != -1){
info.offset = 0;
info.size = length;
info.flags = videoFlag;
info.presentationTimeUs += radioFrameRate;
mediaMuxer.writeSampleData(mVideoTrackIndex, buffer, info);
extractor.advance();
}
extractor.release();
mediaMuxer.stop();
mediaMuxer.release();
showShortToast("音频生成成功");
}catch (Exception e){
e.printStackTrace();
showShortToast("音频生成失败");
}
}
|