android bitmap h.264 android-4.2-jelly-bean video-encoding

android - ¿Cómo codificar Bitmaps en un video usando MediaCodec?



h.264 android-4.2-jelly-bean (4)

  1. La salida de los codificadores es h264 "sin formato", por lo que puede establecer la extensión del nombre del archivo en "h264" y reproducirlo con mplayer, es decir, mplayer ./your_output.h264
  2. Una cosa más: usted dice al codificador que el fotograma estará en COLOR_FormatYUV420Planar el formato de color, pero parece que le da contenido PNG, por lo que el archivo de salida probablemente contenga el desorden de color. Creo que deberías convertir PNG a yuv420 (con esto, por ejemplo, https://code.google.com/p/libyuv/ ) antes de enviarlo al codificador.

Me gustaría codificar un conjunto de mapas de bits que tengo en un h264. ¿Es esto posible a través de MediaEncoder? He escrito un código para hacerlo, pero la salida no se puede reproducir en ningún reproductor multimedia que haya probado. Aquí hay algunos de los códigos que tomé prestados principalmente de otras fuentes que encontré en Stackoverflow.

mMediaCodec = MediaCodec.createEncoderByType("video/avc"); mMediaFormat = MediaFormat.createVideoFormat("video/avc", 320, 240); mMediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 125000); mMediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 15); mMediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar); mMediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5); mMediaCodec.configure(mMediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); mMediaCodec.start(); mInputBuffers = mMediaCodec.getInputBuffers(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); // image is the bitmap byte[] input = byteArrayOutputStream.toByteArray(); int inputBufferIndex = mMediaCodec.dequeueInputBuffer(-1); if (inputBufferIndex >= 0) { ByteBuffer inputBuffer = mInputBuffers[inputBufferIndex]; inputBuffer.clear(); inputBuffer.put(input); mMediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, 0, 0); }

¿Qué debo ajustar?


He modificado el código proporcionado por abalta para aceptar mapas de bits en tiempo real (es decir, no es necesario que los mapas de bits se guarden en el disco). También tiene una mejora en el rendimiento, ya que no necesita escribir, luego leer los mapas de bits del disco. También aumenté el TIMEOUT_USEC del ejemplo original que corrigió algunos errores relacionados con el tiempo de espera que estaba teniendo.

Esperemos que esto ayude a alguien. Pasé mucho tiempo tratando de hacer esto sin tener que empaquetar una gran biblioteca de terceros en mi aplicación (por ejemplo, ffmpeg), así que realmente aprecio la respuesta de abalta.

Estoy usando rxjava, por lo que necesitará esto en las dependencias de build.gradle de su aplicación:

implementation ''io.reactivex.rxjava2:rxandroid:2.0.2''

Si está intentando escribir en un almacenamiento externo, necesitará el permiso de almacenamiento externo definido en su manifiesto:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

y ya sea manualmente alternar el permiso en la aplicación de configuración del sistema para su aplicación, o agregar el manejo de la solicitud de permiso a su actividad.

Y aquí está la clase:

import android.graphics.Bitmap; import android.media.MediaCodec; import android.media.MediaCodecInfo; import android.media.MediaCodecList; import android.media.MediaFormat; import android.media.MediaMuxer; import android.util.Log; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CountDownLatch; import io.reactivex.Completable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; public class BitmapToVideoEncoder { private static final String TAG = BitmapToVideoEncoder.class.getSimpleName(); private IBitmapToVideoEncoderCallback mCallback; private File mOutputFile; private Queue<Bitmap> mEncodeQueue = new ConcurrentLinkedQueue(); private MediaCodec mediaCodec; private MediaMuxer mediaMuxer; private Object mFrameSync = new Object(); private CountDownLatch mNewFrameLatch; private static final String MIME_TYPE = "video/avc"; // H.264 Advanced Video Coding private static int mWidth; private static int mHeight; private static final int BIT_RATE = 16000000; private static final int FRAME_RATE = 30; // Frames per second private static final int I_FRAME_INTERVAL = 1; private int mGenerateIndex = 0; private int mTrackIndex; private boolean mNoMoreFrames = false; private boolean mAbort = false; public interface IBitmapToVideoEncoderCallback { void onEncodingComplete(File outputFile); } public BitmapToVideoEncoder(IBitmapToVideoEncoderCallback callback) { mCallback = callback; } public boolean isEncodingStarted() { return (mediaCodec != null) && (mediaMuxer != null) && !mNoMoreFrames && !mAbort; } public int getActiveBitmaps() { return mEncodeQueue.size(); } public void startEncoding(int width, int height, File outputFile) { mWidth = width; mHeight = height; mOutputFile = outputFile; String outputFileString; try { outputFileString = outputFile.getCanonicalPath(); } catch (IOException e) { Log.e(TAG, "Unable to get path for " + outputFile); return; } MediaCodecInfo codecInfo = selectCodec(MIME_TYPE); if (codecInfo == null) { Log.e(TAG, "Unable to find an appropriate codec for " + MIME_TYPE); return; } Log.d(TAG, "found codec: " + codecInfo.getName()); int colorFormat; try { colorFormat = selectColorFormat(codecInfo, MIME_TYPE); } catch (Exception e) { colorFormat = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar; } try { mediaCodec = MediaCodec.createByCodecName(codecInfo.getName()); } catch (IOException e) { Log.e(TAG, "Unable to create MediaCodec " + e.getMessage()); return; } MediaFormat mediaFormat = MediaFormat.createVideoFormat(MIME_TYPE, mWidth, mHeight); mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE); mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE); mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, colorFormat); mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, I_FRAME_INTERVAL); mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); mediaCodec.start(); try { mediaMuxer = new MediaMuxer(outputFileString, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); } catch (IOException e) { Log.e(TAG,"MediaMuxer creation failed. " + e.getMessage()); return; } Log.d(TAG, "Initialization complete. Starting encoder..."); Completable.fromAction(() -> encode()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(); } public void stopEncoding() { if (mediaCodec == null || mediaMuxer == null) { Log.d(TAG, "Failed to stop encoding since it never started"); return; } Log.d(TAG, "Stopping encoding"); mNoMoreFrames = true; synchronized (mFrameSync) { if ((mNewFrameLatch != null) && (mNewFrameLatch.getCount() > 0)) { mNewFrameLatch.countDown(); } } } public void abortEncoding() { if (mediaCodec == null || mediaMuxer == null) { Log.d(TAG, "Failed to abort encoding since it never started"); return; } Log.d(TAG, "Aborting encoding"); mNoMoreFrames = true; mAbort = true; mEncodeQueue = new ConcurrentLinkedQueue(); // Drop all frames synchronized (mFrameSync) { if ((mNewFrameLatch != null) && (mNewFrameLatch.getCount() > 0)) { mNewFrameLatch.countDown(); } } } public void queueFrame(Bitmap bitmap) { if (mediaCodec == null || mediaMuxer == null) { Log.d(TAG, "Failed to queue frame. Encoding not started"); return; } Log.d(TAG, "Queueing frame"); mEncodeQueue.add(bitmap); synchronized (mFrameSync) { if ((mNewFrameLatch != null) && (mNewFrameLatch.getCount() > 0)) { mNewFrameLatch.countDown(); } } } private void encode() { Log.d(TAG, "Encoder started"); while(true) { if (mNoMoreFrames && (mEncodeQueue.size() == 0)) break; Bitmap bitmap = mEncodeQueue.poll(); if (bitmap == null) { synchronized (mFrameSync) { mNewFrameLatch = new CountDownLatch(1); } try { mNewFrameLatch.await(); } catch (InterruptedException e) {} bitmap = mEncodeQueue.poll(); } if (bitmap == null) continue; byte[] byteConvertFrame = getNV21(bitmap.getWidth(), bitmap.getHeight(), bitmap); long TIMEOUT_USEC = 500000; int inputBufIndex = mediaCodec.dequeueInputBuffer(TIMEOUT_USEC); long ptsUsec = computePresentationTime(mGenerateIndex, FRAME_RATE); if (inputBufIndex >= 0) { final ByteBuffer inputBuffer = mediaCodec.getInputBuffer(inputBufIndex); inputBuffer.clear(); inputBuffer.put(byteConvertFrame); mediaCodec.queueInputBuffer(inputBufIndex, 0, byteConvertFrame.length, ptsUsec, 0); mGenerateIndex++; } MediaCodec.BufferInfo mBufferInfo = new MediaCodec.BufferInfo(); int encoderStatus = mediaCodec.dequeueOutputBuffer(mBufferInfo, TIMEOUT_USEC); if (encoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) { // no output available yet Log.e(TAG, "No output from encoder available"); } else if (encoderStatus == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { // not expected for an encoder MediaFormat newFormat = mediaCodec.getOutputFormat(); mTrackIndex = mediaMuxer.addTrack(newFormat); mediaMuxer.start(); } else if (encoderStatus < 0) { Log.e(TAG, "unexpected result from encoder.dequeueOutputBuffer: " + encoderStatus); } else if (mBufferInfo.size != 0) { ByteBuffer encodedData = mediaCodec.getOutputBuffer(encoderStatus); if (encodedData == null) { Log.e(TAG, "encoderOutputBuffer " + encoderStatus + " was null"); } else { encodedData.position(mBufferInfo.offset); encodedData.limit(mBufferInfo.offset + mBufferInfo.size); mediaMuxer.writeSampleData(mTrackIndex, encodedData, mBufferInfo); mediaCodec.releaseOutputBuffer(encoderStatus, false); } } } release(); if (mAbort) { mOutputFile.delete(); } else { mCallback.onEncodingComplete(mOutputFile); } } private void release() { if (mediaCodec != null) { mediaCodec.stop(); mediaCodec.release(); mediaCodec = null; Log.d(TAG,"RELEASE CODEC"); } if (mediaMuxer != null) { mediaMuxer.stop(); mediaMuxer.release(); mediaMuxer = null; Log.d(TAG,"RELEASE MUXER"); } } private static MediaCodecInfo selectCodec(String mimeType) { int numCodecs = MediaCodecList.getCodecCount(); for (int i = 0; i < numCodecs; i++) { MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i); if (!codecInfo.isEncoder()) { continue; } String[] types = codecInfo.getSupportedTypes(); for (int j = 0; j < types.length; j++) { if (types[j].equalsIgnoreCase(mimeType)) { return codecInfo; } } } return null; } private static int selectColorFormat(MediaCodecInfo codecInfo, String mimeType) { MediaCodecInfo.CodecCapabilities capabilities = codecInfo .getCapabilitiesForType(mimeType); for (int i = 0; i < capabilities.colorFormats.length; i++) { int colorFormat = capabilities.colorFormats[i]; if (isRecognizedFormat(colorFormat)) { return colorFormat; } } return 0; // not reached } private static boolean isRecognizedFormat(int colorFormat) { switch (colorFormat) { // these are the formats we know how to handle for case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar: case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420PackedPlanar: case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar: case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420PackedSemiPlanar: case MediaCodecInfo.CodecCapabilities.COLOR_TI_FormatYUV420PackedSemiPlanar: return true; default: return false; } } private byte[] getNV21(int inputWidth, int inputHeight, Bitmap scaled) { int[] argb = new int[inputWidth * inputHeight]; scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight); byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2]; encodeYUV420SP(yuv, argb, inputWidth, inputHeight); scaled.recycle(); return yuv; } private 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; 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; yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y)); if (j % 2 == 0 && index % 2 == 0) { yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U)); yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V)); } index++; } } } private long computePresentationTime(long frameIndex, int framerate) { return 132 + frameIndex * 1000000 / framerate; } }

El uso es algo como:

BitmapToVideoEncoder bitmapToVideoEncoder = new BitmapToVideoEncoder(new IBitmapToVideoEncoderCallback() { @Override public void onEncodingComplete(File outputFile) { Toast.makeText(this, "Encoding complete!", Toast.LENGTH_LONG); } }); bitmapToVideoEncoder.startEncoding(getWidth(), getHeight(), new File("some_path")); bitmapToVideoEncoder.queueFrame(bitmap1); bitmapToVideoEncoder.queueFrame(bitmap2); bitmapToVideoEncoder.queueFrame(bitmap3); bitmapToVideoEncoder.queueFrame(bitmap4); bitmapToVideoEncoder.queueFrame(bitmap5); bitmapToVideoEncoder.stopEncoding();

Y si se interrumpe su grabación (la actividad anterior está en pausa), puede abortar y eliminará el archivo (ya que de todos modos estaría dañado). Alternativamente, solo llame a stopEncoding y cerrará correctamente el archivo para que no esté dañado:

bitmapToVideoEncoder.abortEncoding();

También hay una función getActiveBitmaps () para ver qué tan grande es la cola (si la cola es demasiado grande, puede quedarse sin memoria). También aquí hay algo de código para crear de manera eficiente un mapa de bits desde una vista para que pueda ponerlo en cola (mi aplicación toma capturas de pantalla periódicas y las codifica en un video):

View view = some_view; final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); // Create a handler thread to offload the processing of the image. final HandlerThread handlerThread = new HandlerThread("PixelCopier"); handlerThread.start(); PixelCopy.request(view, bitmap, (copyResult) -> { bitmapToVideoEncoder.queueFrame(bitmap); }, new Handler(handlerThread.getLooper()));


La salida de MediaCodec es un flujo elemental H.264 sin formato. Descubrí que el reproductor multimedia Totem para Linux puede reproducirlos.

Lo que realmente quieres hacer es convertir la salida en un archivo .mp4. ( Actualización MediaMuxer Android 4.3 (API 18) introdujo la clase MediaMuxer , que proporciona una forma de convertir los datos sin procesar (más una transmisión de audio opcional) a un archivo .mp4.

El diseño de los datos en ByteBuffer es, a partir de Android 4.3, dependiente del dispositivo. (De hecho, no todos los dispositivos son compatibles con COLOR_FormatYUV420Planar ; algunos prefieren una variante semiplanar). No puedo decirle el diseño exacto sin conocer su dispositivo, pero puedo decirle que desea datos sin comprimir, por lo que pasa en formato PNG comprimido. Los datos no van a funcionar.

( Actualización :) Android 4.3 también permite la entrada de Surface a los codificadores MediaCodec , para que se pueda grabar cualquier cosa que se pueda procesar con OpenGL ES. Para ver un ejemplo, vea el ejemplo de EncodeAndMuxTest here .


Utilicé los siguientes pasos para convertir mis mapas de bits en un archivo de video.

Paso 1: Preparando

He preparado un codificador como este. Yo uso MediaMuxer para crear un archivo mp4.

private void prepareEncoder() { try { mBufferInfo = new MediaCodec.BufferInfo(); mediaCodec = MediaCodec.createEncoderByType(MIME_TYPE); mediaFormat = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT); mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, calcBitRate()); mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE); if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) { mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar); }else{ mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible); } //2130708361, 2135033992, 21 mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL); final MediaFormat audioFormat = MediaFormat.createAudioFormat(MIME_TYPE_AUDIO, SAMPLE_RATE, 1); audioFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC); audioFormat.setInteger(MediaFormat.KEY_CHANNEL_MASK, AudioFormat.CHANNEL_IN_MONO); audioFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE); audioFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1); mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); mediaCodec.start(); mediaCodecForAudio = MediaCodec.createEncoderByType(MIME_TYPE_AUDIO); mediaCodecForAudio.configure(audioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); mediaCodecForAudio.start(); try { String outputPath = new File(Environment.getExternalStorageDirectory(), "test.mp4").toString(); mediaMuxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); } catch (IOException ioe) { throw new RuntimeException("MediaMuxer creation failed", ioe); } } catch (IOException e) { e.printStackTrace(); } }

Paso 2: Buffering

He creado ejecutable para el almacenamiento en búfer.

private void bufferEncoder() { runnable = new Runnable() { @Override public void run() { prepareEncoder(); try { while (mRunning) { encode(); } encode(); } finally { release(); } } }; Thread thread = new Thread(runnable); thread.start(); }

Paso 3: Codificación

Esta es la parte más importante que te has perdido. En esta parte, he preparado el buffer de entrada antes de la salida. Cuando los buffers de entrada están en cola, los buffers de salida están listos para la codificación.

public void encode() { while (true) { if (!mRunning) { break; } int inputBufIndex = mediaCodec.dequeueInputBuffer(TIMEOUT_USEC); long ptsUsec = computePresentationTime(generateIndex); if (inputBufIndex >= 0) { Bitmap image = loadBitmapFromView(captureImageView); image = Bitmap.createScaledBitmap(image, WIDTH, HEIGHT, false); byte[] input = getNV21(WIDTH, HEIGHT, image); final ByteBuffer inputBuffer = mediaCodec.getInputBuffer(inputBufIndex); inputBuffer.clear(); inputBuffer.put(input); mediaCodec.queueInputBuffer(inputBufIndex, 0, input.length, ptsUsec, 0); generateIndex++; } int encoderStatus = mediaCodec.dequeueOutputBuffer(mBufferInfo, TIMEOUT_USEC); if (encoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) { // no output available yet Log.d("CODEC", "no output from encoder available"); } else if (encoderStatus == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { // not expected for an encoder MediaFormat newFormat = mediaCodec.getOutputFormat(); mTrackIndex = mediaMuxer.addTrack(newFormat); mediaMuxer.start(); } else if (encoderStatus < 0) { Log.i("CODEC", "unexpected result from encoder.dequeueOutputBuffer: " + encoderStatus); } else if (mBufferInfo.size != 0) { ByteBuffer encodedData = mediaCodec.getOutputBuffer(encoderStatus); if (encodedData == null) { Log.i("CODEC", "encoderOutputBuffer " + encoderStatus + " was null"); } else { encodedData.position(mBufferInfo.offset); encodedData.limit(mBufferInfo.offset + mBufferInfo.size); mediaMuxer.writeSampleData(mTrackIndex, encodedData, mBufferInfo); mediaCodec.releaseOutputBuffer(encoderStatus, false); } } } } }

Paso 4: Liberar

Finalmente, si terminamos de codificar, soltamos el muxer y el codificador.

private void release() { if (mediaCodec != null) { mediaCodec.stop(); mediaCodec.release(); mediaCodec = null; Log.i("CODEC", "RELEASE CODEC"); } if (mediaMuxer != null) { mediaMuxer.stop(); mediaMuxer.release(); mediaMuxer = null; Log.i("CODEC", "RELEASE MUXER"); } }

Espero que esto te ayude.