222 lines
8.7 KiB
Java
222 lines
8.7 KiB
Java
package com.hw.videoprocessor;
|
|
|
|
import android.annotation.TargetApi;
|
|
import android.content.Context;
|
|
import android.media.MediaExtractor;
|
|
import android.media.MediaFormat;
|
|
import android.media.MediaMetadataRetriever;
|
|
import android.media.MediaMuxer;
|
|
import com.hw.videoprocessor.util.AudioUtil;
|
|
import com.hw.videoprocessor.util.CL;
|
|
import com.hw.videoprocessor.util.VideoProgressAve;
|
|
import com.hw.videoprocessor.util.VideoProgressListener;
|
|
import java.util.concurrent.CountDownLatch;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
@TargetApi(21)
|
|
/* loaded from: classes.dex */
|
|
public class VideoProcessor {
|
|
public static int a = 20;
|
|
|
|
public static class Processor {
|
|
private Context a;
|
|
private String b;
|
|
private String c;
|
|
private Integer d;
|
|
private Integer e;
|
|
private Integer f;
|
|
private Integer g;
|
|
private Float h;
|
|
private Boolean i;
|
|
private Integer j;
|
|
private Integer k;
|
|
private Integer l;
|
|
private VideoProgressListener m;
|
|
private boolean n = true;
|
|
|
|
public Processor(Context context) {
|
|
this.a = context;
|
|
}
|
|
|
|
public Processor a(String str) {
|
|
this.b = str;
|
|
return this;
|
|
}
|
|
|
|
public Processor b(String str) {
|
|
this.c = str;
|
|
return this;
|
|
}
|
|
|
|
public Processor a(int i) {
|
|
this.j = Integer.valueOf(i);
|
|
return this;
|
|
}
|
|
|
|
public Processor a(VideoProgressListener videoProgressListener) {
|
|
this.m = videoProgressListener;
|
|
return this;
|
|
}
|
|
|
|
public void a() throws Exception {
|
|
VideoProcessor.a(this.a, this);
|
|
}
|
|
}
|
|
|
|
public static void a(Context context, Processor processor) throws Exception {
|
|
int i;
|
|
int i2;
|
|
MediaMuxer mediaMuxer;
|
|
Integer num;
|
|
int i3;
|
|
MediaMuxer mediaMuxer2;
|
|
long j;
|
|
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
|
|
mediaMetadataRetriever.setDataSource(processor.b);
|
|
int parseInt = Integer.parseInt(mediaMetadataRetriever.extractMetadata(18));
|
|
int parseInt2 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(19));
|
|
int parseInt3 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(24));
|
|
int parseInt4 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(20));
|
|
int parseInt5 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(9));
|
|
mediaMetadataRetriever.release();
|
|
if (processor.j == null) {
|
|
processor.j = Integer.valueOf(parseInt4);
|
|
}
|
|
if (processor.l == null) {
|
|
processor.l = 1;
|
|
}
|
|
if (processor.d != null) {
|
|
parseInt = processor.d.intValue();
|
|
}
|
|
if (processor.e != null) {
|
|
parseInt2 = processor.e.intValue();
|
|
}
|
|
if (parseInt % 2 != 0) {
|
|
parseInt++;
|
|
}
|
|
if (parseInt2 % 2 != 0) {
|
|
parseInt2++;
|
|
}
|
|
if (parseInt3 == 90 || parseInt3 == 270) {
|
|
i = parseInt;
|
|
i2 = parseInt2;
|
|
} else {
|
|
i2 = parseInt;
|
|
i = parseInt2;
|
|
}
|
|
MediaExtractor mediaExtractor = new MediaExtractor();
|
|
mediaExtractor.setDataSource(processor.b);
|
|
int a2 = VideoUtil.a(mediaExtractor, false);
|
|
int a3 = VideoUtil.a(mediaExtractor, true);
|
|
MediaMuxer mediaMuxer3 = new MediaMuxer(processor.c, 0);
|
|
boolean booleanValue = processor.i == null ? true : processor.i.booleanValue();
|
|
Integer num2 = processor.g;
|
|
if (a3 >= 0) {
|
|
MediaFormat trackFormat = mediaExtractor.getTrackFormat(a3);
|
|
int a4 = AudioUtil.a(trackFormat);
|
|
int integer = trackFormat.getInteger("channel-count");
|
|
int integer2 = trackFormat.getInteger("sample-rate");
|
|
int b = AudioUtil.b(trackFormat);
|
|
MediaFormat createAudioFormat = MediaFormat.createAudioFormat("audio/mp4a-latm", integer2, integer);
|
|
createAudioFormat.setInteger("bitrate", a4);
|
|
createAudioFormat.setInteger("aac-profile", 2);
|
|
createAudioFormat.setInteger("max-input-size", b);
|
|
if (!booleanValue) {
|
|
mediaMuxer2 = mediaMuxer3;
|
|
long j2 = parseInt5 * 1000;
|
|
long j3 = trackFormat.getLong("durationUs");
|
|
if (processor.f != null || processor.g != null || processor.h != null) {
|
|
if (processor.f != null && processor.g != null) {
|
|
j2 = (processor.g.intValue() - processor.f.intValue()) * 1000;
|
|
}
|
|
long floatValue = processor.h != null ? (long) (j2 / processor.h.floatValue()) : j2;
|
|
if (floatValue >= j3) {
|
|
floatValue = j3;
|
|
}
|
|
createAudioFormat.setLong("durationUs", floatValue);
|
|
num2 = Integer.valueOf((processor.f == null ? 0 : processor.f.intValue()) + ((int) (floatValue / 1000)));
|
|
}
|
|
} else if (processor.f == null && processor.g == null && processor.h == null) {
|
|
mediaMuxer2 = mediaMuxer3;
|
|
} else {
|
|
long j4 = trackFormat.getLong("durationUs");
|
|
if (processor.f == null || processor.g == null) {
|
|
mediaMuxer2 = mediaMuxer3;
|
|
j = j4;
|
|
} else {
|
|
mediaMuxer2 = mediaMuxer3;
|
|
j = (processor.g.intValue() - processor.f.intValue()) * 1000;
|
|
}
|
|
if (processor.h != null) {
|
|
j = (long) (j / processor.h.floatValue());
|
|
}
|
|
createAudioFormat.setLong("durationUs", j);
|
|
}
|
|
AudioUtil.a(createAudioFormat, 2, integer2, integer);
|
|
mediaMuxer = mediaMuxer2;
|
|
i3 = mediaMuxer.addTrack(createAudioFormat);
|
|
num = num2;
|
|
} else {
|
|
mediaMuxer = mediaMuxer3;
|
|
num = num2;
|
|
i3 = 0;
|
|
}
|
|
mediaExtractor.selectTrack(a2);
|
|
if (processor.f != null) {
|
|
mediaExtractor.seekTo(processor.f.intValue() * 1000, 0);
|
|
} else {
|
|
mediaExtractor.seekTo(0L, 0);
|
|
}
|
|
VideoProgressAve videoProgressAve = new VideoProgressAve(processor.m);
|
|
videoProgressAve.a(processor.h);
|
|
videoProgressAve.b(processor.f == null ? 0 : processor.f.intValue());
|
|
if (processor.g != null) {
|
|
parseInt5 = processor.g.intValue();
|
|
}
|
|
videoProgressAve.a(parseInt5);
|
|
AtomicBoolean atomicBoolean = new AtomicBoolean(false);
|
|
CountDownLatch countDownLatch = new CountDownLatch(1);
|
|
VideoEncodeThread videoEncodeThread = new VideoEncodeThread(mediaExtractor, mediaMuxer, processor.j.intValue(), i2, i, processor.l.intValue(), processor.k == null ? a : processor.k.intValue(), a2, atomicBoolean, countDownLatch);
|
|
int b2 = VideoUtil.b(processor.b);
|
|
if (b2 <= 0) {
|
|
b2 = (int) Math.ceil(VideoUtil.a(processor.b));
|
|
}
|
|
VideoDecodeThread videoDecodeThread = new VideoDecodeThread(videoEncodeThread, mediaExtractor, processor.f, processor.g, Integer.valueOf(b2), Integer.valueOf(processor.k == null ? a : processor.k.intValue()), processor.h, processor.n, a2, atomicBoolean);
|
|
AudioProcessThread audioProcessThread = new AudioProcessThread(context, processor.b, mediaMuxer, processor.f, num, booleanValue ? processor.h : null, i3, countDownLatch);
|
|
videoEncodeThread.a(videoProgressAve);
|
|
audioProcessThread.a(videoProgressAve);
|
|
videoDecodeThread.start();
|
|
videoEncodeThread.start();
|
|
audioProcessThread.start();
|
|
try {
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
videoDecodeThread.join();
|
|
videoEncodeThread.join();
|
|
long currentTimeMillis2 = System.currentTimeMillis();
|
|
audioProcessThread.join();
|
|
CL.d(String.format("编解码:%dms,音频:%dms", Long.valueOf(currentTimeMillis2 - currentTimeMillis), Long.valueOf(System.currentTimeMillis() - currentTimeMillis)), new Object[0]);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
try {
|
|
mediaMuxer.release();
|
|
mediaExtractor.release();
|
|
} catch (Exception e2) {
|
|
CL.a(e2);
|
|
}
|
|
if (videoEncodeThread.c() != null) {
|
|
throw videoEncodeThread.c();
|
|
}
|
|
if (videoDecodeThread.a() != null) {
|
|
throw videoDecodeThread.a();
|
|
}
|
|
if (audioProcessThread.a() != null) {
|
|
throw audioProcessThread.a();
|
|
}
|
|
}
|
|
|
|
public static Processor a(Context context) {
|
|
return new Processor(context);
|
|
}
|
|
}
|