Android 비디오 처리 의 동적 시간 워 터 마크 효과
마지막 으로 생각 나 는 방법 은 녹화 가 끝 난 후에 이 동 영상 을 처리 하 는 것 이다.
그동안 많은 자 료 를 참고 하 였 는데,비교적 유용 한 것 은 아마도 ffmpeg 과 비교적 새로운 Api mediaCodec 시리즈 일 것 이다.ffmpeg 는 모두 C 로 이 루어 졌 고 많은 NDK 와 관련 되 어 있 으 며 본인 은 잘 모 르 기 때문에 MediaCodec 시리즈 에 중점 을 두 었 습 니 다.
논리 적 흐름 도 를 참고 하여 일목요연 한 이 박문
MediaCodec 에서 디 코딩 을 하 는 대체적인 논 리 는 다음 과 같 습 니 다(전재).
주요 함수 의 호출 논 리 는 다음 과 같다.
MediaExtractor,MediaCodec,MediaMuxer 등 세 개의 Api 는 이미 많은 멀티미디어 처리 작업 을 할 수 있다.예 를 들 어 MediaExtractor+MediaMuxer 로 음성 영상 편집 을 할 수 있 고 MediaCodec+MediaMuxer 는 사용자 정의 비디오 를 만 들 수 있 으 며 함께 사용 하면 특수효과 편집,필터 등 을 할 수 있다.
시간 워 터 마크 효과 추가
관건 은 데이터 프레임 을 얻 는 것 입 니 다.YUV 형식 입 니 다.촬영 할 때 선택 한 것 에 따라 다 릅 니 다.제 가 사용 하 는 NV 21 형식,즉 YUV 420 sp 입 니 다.NV 21 형식의 프레임 을 받 은 후에 RGB 렌 더 링 으로 전환 한 다음 에 NV 21 을 encoder 에 게 건 네 주 었 습 니 다.너무 무 겁 고 시간 이 많이 걸 렸 지만 저 는 더 좋 은 방법 을 찾 지 못 했 습 니 다.
private Bitmap first;
private void handleFrameData(byte[] data, MediaCodec.BufferInfo info) {
//YUV420sp RGB 5-60ms
ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, srcWidth, srcHeight, null);
yuvImage.compressToJpeg(new Rect(0, 0, srcWidth, srcHeight), 100, out);
byte[] imageBytes = out.toByteArray();
// , 90 20-50ms
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
Bitmap bitmap = rotaingImageView(videoRotation, image);
image.recycle();
// 0-1ms
Canvas canvas = new Canvas(bitmap);
canvas.drawText(videoTimeFormat.format(videoFirstTime + info.presentationTimeUs / 1000), 10, 30, paint);
// 0-5ms
first = bitmap;
handler.sendEmptyMessage((int) (info.presentationTimeUs / 1000));
synchronized (MediaCodec.class) {//
timeDataContainer.add(new Frame(info, bitmap));
}
}
/*
*
* @param angle
* @param bitmap
* @return Bitmap
*/
public Bitmap rotaingImageView(int angle, Bitmap bitmap) {
//
Matrix matrix = new Matrix();
matrix.postRotate(angle);
//
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
그다음에 NV 21 로 돌아 가 겠 습 니 다.
/**
*
*
* @return
*/
private Frame getFrameData() {
synchronized (MediaCodec.class) {//
if (timeDataContainer.isEmpty()) {
return null;
}
//
Frame frame = timeDataContainer.remove(0);//// remove PCM
// YUV420sp 120-160ms
frame.data = getNV21(dstWidth, dstHeight, frame.bitmap);
return frame;
}
}
public static byte[] getNV21(int width, int height, Bitmap scaled) {
int[] argb = new int[width * height];
scaled.getPixels(argb, 0, width, 0, 0, width, height);
byte[] yuv = new byte[width * height * 3 / 2];
encodeYUV420SP(yuv, argb, width, height);
scaled.recycle();
return yuv;
}
/**
* bitmap argb yuv420sp
* yuv420sp MediaCodec, AvcEncoder
*
* @param yuv420sp yuv420sp
* @param argb argb
* @param width width
* @param height height
*/
public static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int a, R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
// a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff) >> 0;
// well known RGB to YUV algorithm
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other
// pixel AND every other scanline.
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
}
index++;
}
}
}
위의 코드 가 실행 되 는 데 걸 리 는 시간 을 보면 실시 간 으로 녹화 할 때 처리 할 수 없습니다.백 스테이지 서비스 가 처리 되 더 라 도 3 초의 720*480 영상 은 약 20 초 걸 립 니 다.디 코딩 과 인 코딩 하 는 곳 도 많 습 니 다.예 를 들 어 인 코더 가 일부 휴대 전화 에서 색상 형식 을 지원 하지 않 습 니 다.더 많은 기종 에 맞 게 색상 형식 을 설정 한 후에 바 꿔 야 합 니 다.
/**
*
*/
private void initMediaEncode(String mime) {
try {
MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, dstWidth, dstHeight);
format.setInteger(MediaFormat.KEY_BIT_RATE, 1024 * 512);
format.setInteger(MediaFormat.KEY_FRAME_RATE, 27);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible);
// format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
mediaEncode = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
mediaEncode.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
} catch (IOException e) {
e.printStackTrace();
}
if (mediaEncode == null) {
JLog.e(tag, "create mediaEncode failed");
return;
}
mediaEncode.start();
}
보충:대부분의 휴대 전화 에 맞 는 색상 모드 는 MediaCodecInfo.CodecCapabilities.COLORFormatYUV 420 SemiPlanar=21,이 색상 형식 은 decode 디 코딩 첫 번 째 buffer 에서 얻 은 것 입 니 다.그러나 이 format 는 인 코딩 률,키 프레임 간격,FPS 등 이 없습니다.이러한 것들 은 자신의 상황 에 따라 만 설정 할 수 있 습 니 다.왜 내 가 전에 YUV 420 Flexible 을 사 용 했 는 지,안 드 로 이 드 소스 코드 에서 말 한 YUV 420 SemiPlanner 를 버 렸 는 지
@deprecated Use {@link #COLOR_FormatYUV420Flexible}。
public static final int COLOR_FormatYUV420SemiPlanar = 21;
하지만 이 제 는 원본 파일 의 첫 번 째 buffer 에서 디 코딩 을 읽 을 수 있 습 니 다.
case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
MediaFormat format = mediaDecode.getOutputFormat();
Log.d(tag, "New format " + format);
if (format != null && format.containsKey(MediaFormat.KEY_COLOR_FORMAT)) {
videoColorFormat = format.getInteger(MediaFormat.KEY_COLOR_FORMAT);
Log.d(tag, "decode extract get videoColorFormat =" + videoColorFormat);//
}
initMediaEncode(videoColorFormat);//
break;
원본 코드:
import android.annotation.TargetApi;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMetadataRetriever;
import android.media.MediaMuxer;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
* Created by user on 2016/8/13.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class TestCodecService extends Service {
private MediaExtractor extractor;
private MediaMuxer muxer;
private final static String tag = "px";
private final String TAG = this.getClass().getSimpleName();
private MediaFormat format;
private int videoMaxInputSize = 0, videoRotation = 0;
private long videoDuration;
private boolean decodeOver = false, encoding = false, mCancel, mDelete;
//
private int videoTrackIndex = -1;
private MediaCodec mediaDecode, mediaEncode;
private ByteBuffer[] decodeInputBuffers, decodeOutputBuffers;
private ArrayList<Frame> timeDataContainer;//
private MediaCodec.BufferInfo decodeBufferInfo;
private int srcWidth, srcHeight, dstWidth, dstHeight;
private SimpleDateFormat videoTimeFormat;
private int mProgress, mMax;
private VideoCodecDao codecDao;
//
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
@Override
public void onCreate() {
super.onCreate();
JLog.d(TAG, "onCreate");
//
videoTimeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
timeDataContainer = new ArrayList<>();
//
paint.setColor(Color.WHITE);
paint.setTextSize(20);
codecDao = VideoCodecDao.getInstance(JingRuiApp.getJRApplicationContext());
}
@Override
public void onDestroy() {
super.onDestroy();
JLog.d(TAG, "onDestroy");
decodeOver = true;
encoding = false;
}
private void init(String srcPath, String dstpath) {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(srcPath);
try {
srcWidth = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
srcHeight = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
try {
extractor = new MediaExtractor();
extractor.setDataSource(srcPath);
String mime = null;
for (int i = 0; i < extractor.getTrackCount(); i++) {
// /
MediaFormat format = extractor.getTrackFormat(i);
mime = format.getString(MediaFormat.KEY_MIME);
if (mime.startsWith("video/")) {
videoTrackIndex = i;
this.format = format;
} else if (mime.startsWith("audio/")) {
continue;
} else {
continue;
}
}
extractor.selectTrack(videoTrackIndex); //
//
srcWidth = format.getInteger(MediaFormat.KEY_WIDTH);
dstHeight = format.getInteger(MediaFormat.KEY_HEIGHT);
videoMaxInputSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
videoDuration = format.getLong(MediaFormat.KEY_DURATION);
//videoRotation = format.getInteger(MediaFormat.KEY_ROTATION);
videoRotation = 90;// ,
if (videoRotation == 90) {
dstWidth = srcHeight;
dstHeight = srcWidth;
} else if (videoRotation == 0) {
dstWidth = srcWidth;
dstHeight = srcHeight;
}
mMax = (int) (videoDuration / 1000);
//int bit = this.format.getInteger(MediaFormat.KEY_BIT_RATE);
JLog.d(tag, "videoWidth=" + srcWidth + ",videoHeight=" + srcHeight + ",videoMaxInputSize=" + videoMaxInputSize + ",videoDuration=" + videoDuration + ",videoRotation=" + videoRotation);
//
muxer = new MediaMuxer(dstpath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
//
//videoTrackIndex = muxer.addTrack(format);
MediaCodec.BufferInfo videoInfo = new MediaCodec.BufferInfo();
videoInfo.presentationTimeUs = 0;
initMediaDecode(mime);
initMediaEncode(mime);
} catch (IOException e) {
e.printStackTrace();
}
}
//
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void extract() {
int inputIndex = mediaDecode.dequeueInputBuffer(-1);// inputBuffer -1 ,0 -1,
if (inputIndex < 0) {
JLog.d("px", "=========== code over =======");
return;
}
ByteBuffer inputBuffer = decodeInputBuffers[inputIndex];// inputBuffer
inputBuffer.clear();
int length = extractor.readSampleData(inputBuffer, 0); // ,
if (length < 0) {
JLog.d("px", "extract Over");
decodeOver = true;
return;
} else {
//
long presentationTimeUs = extractor.getSampleTime();
MediaCodec.BufferInfo videoInfo = new MediaCodec.BufferInfo();
videoInfo.offset = 0;
videoInfo.size = length;
// , I
videoInfo.flags = extractor.getSampleFlags();
videoInfo.presentationTimeUs = extractor.getSampleTime();
//
decode(videoInfo, inputIndex);
extractor.advance(); //
}
}
private void handleFrameData(byte[] data, MediaCodec.BufferInfo info) {
//YUV420sp RGB 5-60ms
ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, srcWidth, srcHeight, null);
yuvImage.compressToJpeg(new Rect(0, 0, srcWidth, srcHeight), 100, out);
byte[] imageBytes = out.toByteArray();
// 20-50ms
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
Bitmap bitmap = rotaingImageView(videoRotation, image);
image.recycle();
// 0-1ms
Canvas canvas = new Canvas(bitmap);
canvas.drawText(videoTimeFormat.format(mVideo.videoCreateTime + info.presentationTimeUs / 1000), 10, 30, paint);
// 0-5ms
mProgress = (int) (info.presentationTimeUs / 1000);
if (mListener != null) {
mListener.onProgress(mProgress, mMax);
}
synchronized (MediaCodec.class) {//
timeDataContainer.add(new Frame(info, bitmap));
}
}
public static byte[] getNV21(int width, int height, Bitmap scaled) {
int[] argb = new int[width * height];
scaled.getPixels(argb, 0, width, 0, 0, width, height);
byte[] yuv = new byte[width * height * 3 / 2];
encodeYUV420SP(yuv, argb, width, height);
scaled.recycle();
return yuv;
}
/**
* bitmap argb yuv420sp
* yuv420sp MediaCodec, AvcEncoder
*
* @param yuv420sp yuv420sp
* @param argb argb
* @param width width
* @param height height
*/
public static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
final int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int a, R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
// a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
R = (argb[index] & 0xff0000) >> 16;
G = (argb[index] & 0xff00) >> 8;
B = (argb[index] & 0xff) >> 0;
// well known RGB to YUV algorithm
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other
// pixel AND every other scanline.
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && index % 2 == 0) {
yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
}
index++;
}
}
}
/**
*
*
* @return
*/
private Frame getFrameData() {
synchronized (MediaCodec.class) {//
if (timeDataContainer.isEmpty()) {
return null;
}
//
Frame frame = timeDataContainer.remove(0);//// remove PCM
// YUV420sp 120-160ms
frame.data = getNV21(dstWidth, dstHeight, frame.bitmap);
return frame;
}
}
/*
*
* @param angle
* @param bitmap
* @return Bitmap
*/
public Bitmap rotaingImageView(int angle, Bitmap bitmap) {
//
Matrix matrix = new Matrix();
matrix.postRotate(angle);
//
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
/**
*
*/
private void initMediaDecode(String mime) {
try {
//
mediaDecode = MediaCodec.createDecoderByType(mime);
mediaDecode.configure(format, null, null, 0);
} catch (IOException e) {
e.printStackTrace();
}
if (mediaDecode == null) {
JLog.e(tag, "create mediaDecode failed");
return;
}
mediaDecode.start();
decodeInputBuffers = mediaDecode.getInputBuffers();
decodeOutputBuffers = mediaDecode.getOutputBuffers();
decodeBufferInfo = new MediaCodec.BufferInfo();// byte[]
}
/**
*
*/
private void initMediaEncode(String mime) {
try {
MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, dstWidth, dstHeight);
format.setInteger(MediaFormat.KEY_BIT_RATE, 1024 * 512);
format.setInteger(MediaFormat.KEY_FRAME_RATE, 27);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible);
// format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
mediaEncode = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
mediaEncode.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
} catch (IOException e) {
e.printStackTrace();
}
if (mediaEncode == null) {
JLog.e(tag, "create mediaEncode failed");
return;
}
mediaEncode.start();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void decode(MediaCodec.BufferInfo videoInfo, int inputIndex) {
mediaDecode.queueInputBuffer(inputIndex, 0, videoInfo.size, videoInfo.presentationTimeUs, videoInfo.flags);// MediaDecode
// byte[] BufferInfo 10000 -1 ,0 。
// -1 ,
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputIndex = mediaDecode.dequeueOutputBuffer(bufferInfo, 50000);
switch (outputIndex) {
case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
JLog.d(tag, "INFO_OUTPUT_BUFFERS_CHANGED");
decodeOutputBuffers = mediaDecode.getOutputBuffers();
break;
case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
JLog.d(tag, "New format " + mediaDecode.getOutputFormat());
break;
case MediaCodec.INFO_TRY_AGAIN_LATER:
JLog.d(tag, "dequeueOutputBuffer timed out!");
break;
default:
ByteBuffer outputBuffer;
byte[] frame;
while (outputIndex >= 0) {// while ,
outputBuffer = decodeOutputBuffers[outputIndex];// PCM Buffer
frame = new byte[bufferInfo.size];//BufferInfo
outputBuffer.get(frame);// Buffer
outputBuffer.clear();// Buffer MediaCodec Buffer ,
handleFrameData(frame, videoInfo);// , ,
mediaDecode.releaseOutputBuffer(outputIndex, false);// , MediaCodec Buffer
outputIndex = mediaDecode.dequeueOutputBuffer(decodeBufferInfo, 50000);// , outputIndex=-1
}
break;
}
}
/**
*
*/
private void encode() {
//
byte[] chunkTime;
Frame frame = getFrameData();
if (frame == null) {
return;
}
chunkTime = frame.data;
int inputIndex = mediaEncode.dequeueInputBuffer(-1);//
if (inputIndex < 0) {
JLog.d("px", "dequeueInputBuffer return inputIndex " + inputIndex + ",then break");
mediaEncode.signalEndOfInputStream();
}
ByteBuffer inputBuffer = mediaEncode.getInputBuffers()[inputIndex];//
inputBuffer.clear();//
inputBuffer.put(chunkTime);//PCM inputBuffer
inputBuffer.limit(frame.videoInfo.size);
mediaEncode.queueInputBuffer(inputIndex, 0, chunkTime.length, frame.videoInfo.presentationTimeUs, frame.videoInfo.flags);//
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputIndex = mediaEncode.dequeueOutputBuffer(bufferInfo, 50000);//
switch (outputIndex) {
case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
JLog.d(tag, "INFO_OUTPUT_BUFFERS_CHANGED");
break;
case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
MediaFormat outputFormat = mediaEncode.getOutputFormat();
outputFormat.setInteger(MediaFormat.KEY_ROTATION, videoRotation);
JLog.d(tag, "mediaEncode find New format " + outputFormat);
//
videoTrackIndex = muxer.addTrack(outputFormat);
muxer.start();
break;
case MediaCodec.INFO_TRY_AGAIN_LATER:
JLog.d(tag, "dequeueOutputBuffer timed out!");
break;
default:
ByteBuffer outputBuffer;
while (outputIndex >= 0) {//
outputBuffer = mediaEncode.getOutputBuffers()[outputIndex];// Buffer
muxer.writeSampleData(videoTrackIndex, outputBuffer, bufferInfo);
// JLog.d("px", "writeSampleData:" + bufferInfo.size);
mediaEncode.releaseOutputBuffer(outputIndex, false);
outputIndex = mediaEncode.dequeueOutputBuffer(bufferInfo, 50000);
}
break;
}
}
private void release() {
// MediaMuxer MediaExtractor
extractor.release();
mediaDecode.release();
mediaEncode.release();
muxer.stop();
muxer.release();
}
private DecodeRunnable decodeRunnable;
private EncodeRunnable encodeRunnable;
/**
*
*/
private class DecodeRunnable extends Thread {
@Override
public void run() {
decodeOver = false;
while (!decodeOver) {
try {
extract();
} catch (Exception e) {
//
JLog.e("px", e.toString());
}
synchronized (encodeRunnable) {
encodeRunnable.notify();
}
}
}
}
/**
*
*/
private class EncodeRunnable extends Thread {
@Override
public void run() {
encoding = true;
while (encoding) {
if (timeDataContainer.isEmpty()) {
if (decodeOver) {// ,
break;
}
try {
synchronized (encodeRunnable) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
encode();
}
}
release();
encoding = false;
handler.sendEmptyMessage(-2);//
}
}
android.os.Handler handler = new android.os.Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case -2:
onComplete();
break;
default:
break;
}
}
};
public void onComplete() {
if (mDelete) {//delete , cancel
mDelete = false;
new File(mVideo.srcPath).delete();// , ,
codecDao.deleteItem(mVideo);
JLog.d("px", "delete file " + mVideo.srcPath);
} else {
mVideo.finish = mCancel ? 0 : 100;
codecDao.createOrUpdate(mVideo);// ,
}
if (mCancel) {//
mCancel = false;
new File(mVideo.dstPath).delete();// ,
JLog.d("px", "delete file " + mVideo.dstPath);
} else {//
new File(mVideo.srcPath).delete();// ,
JLog.d("px", "delete file " + mVideo.srcPath);
}
if (mListener != null) {
mListener.onCodecFinish(mVideo);
}
if (!videos.isEmpty()) {
VideoCodecModel video = videos.remove(0);
start(video);
}
}
class Frame {
MediaCodec.BufferInfo videoInfo;
byte[] data;
Bitmap bitmap;
public Frame(MediaCodec.BufferInfo videoInfo, Bitmap bitmap) {
this.videoInfo = videoInfo;
this.bitmap = bitmap;
}
}
private long getInterval() {
//
long videoSampleTime;
ByteBuffer buffer = ByteBuffer.allocate(1024 * 512);
// 。(1)
extractor.readSampleData(buffer, 0);
//skip first I frame
if (extractor.getSampleFlags() == MediaExtractor.SAMPLE_FLAG_SYNC)
extractor.advance();
extractor.readSampleData(buffer, 0);
long firstVideoPTS = extractor.getSampleTime();
extractor.advance();
extractor.readSampleData(buffer, 0);
long SecondVideoPTS = extractor.getSampleTime();
videoSampleTime = Math.abs(SecondVideoPTS - firstVideoPTS);
JLog.d(tag, "videoSampleTime is " + videoSampleTime);
return videoSampleTime;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
JLog.d(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
if (intent == null) {
return START_NOT_STICKY;
}
int action = intent.getIntExtra("action", 0);
if (action == REQUEST_CODEC) {
VideoCodecModel video = (VideoCodecModel) intent.getSerializableExtra("video");
video = codecDao.addItem(video);
if (!encoding) {
start(video);
} else {
videos.add(video);
}
} else if (action == REQUEST_CODEC_CANCEL) {
VideoCodecModel video = (VideoCodecModel) intent.getSerializableExtra("video");
mDelete = intent.getBooleanExtra("delete", false);//
JLog.d("px", "----- onStartCommand action " + action + " is delete?" + mDelete);
mBinder.cancel(video);
}
return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
JLog.d(TAG, "onBind");
return mBinder;
}
private CodecBinder mBinder = new CodecBinder();
private VideoCodecModel mVideo;
//video
private List<VideoCodecModel> videos = new ArrayList<>();
public static final int REQUEST_CODEC = 0x183;
public static final int REQUEST_CODEC_CANCEL = 0x184;
public class CodecBinder extends Binder {
/**
* @param video
* @return ,
*/
public boolean start(VideoCodecModel video) {
video = codecDao.addItem(video);
if (!encoding) {
TestCodecService.this.start(video);
} else {
videos.add(video);
}
return !encoding;
}
public void setOnProgressChangeListener(OnProgressChangeListener l) {
mListener = l;
}
public VideoCodecModel getCurrentVideo() {
return mVideo;
}
public void cancel(VideoCodecModel video) {
if (mVideo.equals(video)) {//
decodeOver = true;//
encoding = false;//
mCancel = true;//
} else {//
boolean flag = videos.remove(video);
if (flag) {
JLog.d("px", "cancel render task sucess");
} else {
//
JLog.d("px", "cancel render task fail,seems this video not in renderring queen");
}
//
if (mDelete) {
mDelete = false;
new File(video.srcPath).delete();
codecDao.deleteItem(video);
}
}
}
public List<VideoCodecModel> getVideoList() {
return videos;
}
public void removeListener() {
mListener = null;
}
}
private void start(VideoCodecModel video) {
if (video == null) {
return;
}
if (!new File(video.srcPath).exists()) {
Toast.makeText(this, " ", Toast.LENGTH_SHORT).show();
video.finish = -100;
codecDao.createOrUpdate(video);
return;
}
mVideo = video;
if (mListener != null) {
mListener.onCodecStart(mVideo);
}
mVideo.finish = 50;//
codecDao.createOrUpdate(mVideo);
Runnable runnable = new Runnable() {
@Override
public void run() {
init(mVideo.srcPath, mVideo.dstPath);
decodeRunnable = new DecodeRunnable();
decodeRunnable.start();
encodeRunnable = new EncodeRunnable();
encodeRunnable.start();
}
};
AsyncTaskExecutor.getExecutor().execute(runnable);
}
private OnProgressChangeListener mListener;
public interface OnProgressChangeListener {
void onProgress(int progress, int max);
void onCodecStart(VideoCodecModel video);
void onCodecFinish(VideoCodecModel video);
}
}
//
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import java.io.Serializable;
/**
* Created by user on 2016/8/29.
*/
@DatabaseTable(tableName = "video_codec_task")
public class VideoCodecModel implements Serializable {
private static final long serialVersionUID = -1307249622002520298L;
@DatabaseField
public String srcPath;
@DatabaseField
public String dstPath;
@DatabaseField
public long videoCreateTime;
@DatabaseField(generatedId = true)
public int id;
//0 ,50 , ,100 ,-100 ,
@DatabaseField
public int finish = 0;
@DatabaseField
public String serno;
// ,
public boolean select;
public VideoCodecModel(String srcPath, String dstPath, long videoCreateTime) {
this.srcPath = srcPath;
this.videoCreateTime = videoCreateTime;
this.dstPath = dstPath;
}
public VideoCodecModel() {
}
public String getSrcPath() {
return srcPath;
}
public void setSrcPath(String srcPath) {
this.srcPath = srcPath;
}
public String getDstPath() {
return dstPath;
}
public void setDstPath(String dstPath) {
this.dstPath = dstPath;
}
public long getVideoCreateTime() {
return videoCreateTime;
}
public void setVideoCreateTime(long videoCreateTime) {
this.videoCreateTime = videoCreateTime;
}
public boolean isSelect() {
return select;
}
public void setSelect(boolean select) {
this.select = select;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof VideoCodecModel)) return false;
VideoCodecModel that = (VideoCodecModel) o;
if (videoCreateTime != that.videoCreateTime) return false;
if (!srcPath.equals(that.srcPath)) return false;
return dstPath.equals(that.dstPath);
}
}
// , Service Activity,,Activity ,
import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.ProgressBar;
import android.widget.TextView;
import ...
import java.io.File;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by user on 2016/8/29.
*/
public class ShowCodecActivity extends BaseActivity implements TestCodecService.OnProgressChangeListener, View.OnClickListener {
private TextView noneTipsView;
private List<VideoCodecModel> videos = new ArrayList<>(), cordingVideos;
private ListView listView;
private BaseAdapter adapter;
private View firstTips;
@Nullable
VideoCodecModel curShowVideo, curRenderVideo;
TestCodecService.CodecBinder binder;
private ProgressBar progressBar;
ServiceConnection connection;
VideoCodecDao codecDao;
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
private boolean mEditMode = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_codec);
setTitle(" ");
initView();
if (getIntent() != null) {
curShowVideo = (VideoCodecModel) getIntent().getSerializableExtra("video");
}
codecDao = VideoCodecDao.getInstance(this);
final Intent intent = new Intent(this, TestCodecService.class);
connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("px", "onServiceConnected");
binder = (TestCodecService.CodecBinder) service;
binder.setOnProgressChangeListener(ShowCodecActivity.this);
videos.clear();
curRenderVideo = binder.getCurrentVideo();
cordingVideos = binder.getVideoList();
videos.addAll(codecDao.queryAll());
notifyChange();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
private void notifyChange() {
if (adapter == null) {
adapter = new BaseAdapter() {
@Override
public int getCount() {
return videos.size();
}
@Override
public VideoCodecModel getItem(int position) {
return videos.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
if (convertView == null) {
convertView = View.inflate(ShowCodecActivity.this, R.layout.item_show_codec, null);
holder = new Holder();
holder.bar = (ProgressBar) convertView.findViewById(R.id.pb_codec);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.serno = (TextView) convertView.findViewById(R.id.serno);
holder.select = convertView.findViewById(R.id.select);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.operate = (TextView) convertView.findViewById(R.id.operate);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb_select);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
final VideoCodecModel video = getItem(position);
if (video.finish == 100) {
holder.status.setText(" ");
holder.operate.setVisibility(View.VISIBLE);
holder.operate.setText(" ");
} else if (video.finish == -100) {
holder.status.setText(" ");
holder.operate.setVisibility(View.INVISIBLE);
} else if (video.equals(curRenderVideo)) {
progressBar = holder.bar;
holder.status.setText(" ");
holder.operate.setVisibility(View.INVISIBLE);
} else if (cordingVideos.contains(video)) {
holder.status.setText(" ");
holder.operate.setVisibility(View.VISIBLE);
holder.operate.setText(" ");
} else {
holder.status.setText(" ");
holder.operate.setVisibility(View.VISIBLE);
holder.operate.setText(" ");
}
holder.operate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (video.finish == 100) {
operate(holder.status, video);
} else if (video.finish == -100) {
return;
} else if (video.equals(curRenderVideo)) {// ,
return;
} else if (cordingVideos.contains(video)) {// ,
binder.cancel(video);
holder.status.setText(" ");
holder.operate.setVisibility(View.VISIBLE);
holder.operate.setText(" ");
} else {
boolean immedia = binder.start(video);
if (immedia) {
holder.status.setText(" ");
holder.operate.setVisibility(View.INVISIBLE);
} else {
holder.status.setText(" ");
holder.operate.setVisibility(View.VISIBLE);
holder.operate.setText(" ");
}
}
}
});
holder.select.setVisibility(video.equals(curShowVideo) ? View.VISIBLE : View.GONE);
holder.serno.setText(video.serno);
holder.time.setText(dateFormat.format(new Date(video.videoCreateTime)));
holder.checkBox.setVisibility(mEditMode ? View.VISIBLE : View.GONE);
holder.checkBox.setChecked(video.isSelect());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
video.setSelect(isChecked);
}
});
return convertView;
}
};
listView.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
noneTipsView.setVisibility(videos.isEmpty() ? View.VISIBLE : View.GONE);
more.setVisibility(mEditMode ? View.VISIBLE : View.GONE);
back.setVisibility(mEditMode ? View.INVISIBLE : View.VISIBLE);
checkBox.setVisibility(mEditMode ? View.VISIBLE : View.GONE);
}
class Holder {
ProgressBar bar;
TextView status, serno, time, operate;
View select;
CheckBox checkBox;
}
private void initView() {
listView = (ListView) findViewById(R.id.lv_codec);
noneTipsView = (TextView) findViewById(R.id.tv_none);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
VideoCodecModel video = videos.get(position);
operate(view, video);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (mEditMode) {
return false;
}
mEditMode = true;
//
for (VideoCodecModel video : videos) {
if (video.select)
video.select = false;
}
checkBox.setChecked(false);
notifyChange();
return true;
}
});
firstTips = findViewById(R.id.ll_tips);
boolean visable = Preferences.getBoolean("firstShowCodec", true);
firstTips.setVisibility(visable ? View.VISIBLE : View.GONE);
if (visable)
findViewById(R.id.btn_noshow).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Preferences.put("firstShowCodec", false);
firstTips.setVisibility(View.GONE);
}
});
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for (VideoCodecModel model : videos) {
model.setSelect(isChecked);
}
notifyChange();
}
});
more.setText(" ");
more.setOnClickListener(this);
}
private void operate(View view, final VideoCodecModel video) {
if (video.finish != 100) {
return;
}
PopupMenu popupMenu = new PopupMenu(ShowCodecActivity.this, view);
popupMenu.getMenu().add(1, 0, 0, " ");
popupMenu.getMenu().add(1, 1, 1, " ");
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case 0:
previewVideo(video.dstPath);
break;
case 1:
File file = new File(video.dstPath);
if (file.exists()) {
file.delete();
}
codecDao.deleteItem(video);
videos.remove(video);
if (cordingVideos.contains(video)) {
binder.cancel(video);
}
notifyChange();
break;
}
return true;
}
});
popupMenu.show();
}
@Override
public void onProgress(int progress, int max) {
if (progressBar != null) {
progressBar.setMax(max);
progressBar.setProgress(progress);
}
}
@Override
public void onCodecStart(VideoCodecModel video) {
JLog.d("px", "onCodecStart");
curRenderVideo = video;
int index = videos.indexOf(video);
if (index >= 0) {
View child = listView.getChildAt(index);
Holder holder = (Holder) child.getTag();
holder.status.setText(" ");
holder.operate.setVisibility(View.INVISIBLE);
progressBar = holder.bar;
}
}
@Override
public void onCodecFinish(VideoCodecModel video) {
JLog.d("px", "onCodecFinish");
if (progressBar != null) {
progressBar.setProgress(0);
}
int index = videos.indexOf(video);
videos.get(index).finish = 100;
if (index >= 0) {
View child = listView.getChildAt(index);
Holder holder = (Holder) child.getTag();
holder.status.setText(" ");
holder.operate.setVisibility(View.VISIBLE);
holder.operate.setText(" ");
progressBar = null;
}
}
@Override
protected void onDestroy() {
if (binder != null)
binder.removeListener();
unbindService(connection);
super.onDestroy();
}
private void previewVideo(String filePath) {
//
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "video/mp4";
Uri uri = Uri.parse("file://" + filePath);
intent.setDataAndType(uri, type);
startActivity(intent);
}
@Override
public void onBackPressed() {
if (mEditMode) {
mEditMode = false;
notifyChange();
return;
}
super.onBackPressed();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.more:
PopupMenu menu = new PopupMenu(this, v);
// menu.getMenu().add(1, 0, 0, " ");
menu.getMenu().add(1, 1, 1, " ");
menu.getMenu().add(1, 2, 2, " ");
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case 0:
break;
case 1:
deleteSelect();
break;
case 2:
mEditMode = false;
notifyChange();
break;
}
return true;
}
});
menu.show();
break;
}
}
//
private void deleteSelect() {
final ProgressDialog dialog = ProgressDialog.show(this, null, null);
AsyncTask<String, String, Boolean> task = new AsyncTask<String, String, Boolean>() {
@Override
protected Boolean doInBackground(String... params) {
boolean has = false;// ,
for (VideoCodecModel video : videos) {
if (video.select) {
File file;
if (video.finish == 100) {
file = new File(video.dstPath);
} else {
file = new File(video.srcPath);
}
if (file.exists()) {
file.delete();
}
codecDao.deleteItem(video);
if (!has) {
has = true;
}
}
}
if (has) {
videos.clear();
videos.addAll(codecDao.queryAll());
}
return has;
}
@Override
protected void onPostExecute(Boolean s) {
mEditMode = false;
notifyChange();
dialog.dismiss();
}
};
task.executeOnExecutor(AsyncTaskExecutor.getExecutor());
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.