Advertisement
quantumSkill

Untitled

Dec 2nd, 2022
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.26 KB | None | 0 0
  1. ...
  2.  
  3. // Set the desired duration of the video, in seconds.
  4. private static final int DURATION_SECONDS = 10;
  5.  
  6. ...
  7.  
  8. public void createVideo() throws IOException {
  9.     ...
  10.  
  11.     // Encode the video frame by frame.
  12.     ByteBuffer[] videoEncoderInputBuffers = videoEncoder.getInputBuffers();
  13.     ByteBuffer[] videoEncoderOutputBuffers = videoEncoder.getOutputBuffers();
  14.     MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo();
  15.     boolean videoEncoderDone = false;
  16.     int videoGeneratedIndex = 0;
  17.     while (!videoEncoderDone) {
  18.         int videoEncoderInputBufferIndex = videoEncoder.dequeueInputBuffer(-1);
  19.         if (videoEncoderInputBufferIndex >= 0) {
  20.             // Send the frame to the video encoder.
  21.             ByteBuffer videoEncoderInputBuffer = videoEncoderInputBuffers[videoEncoderInputBufferIndex];
  22.             videoEncoderInputBuffer.clear();
  23.             long presentationTime = videoGeneratedIndex * 1000000 / FRAME_RATE;
  24.             videoEncoder.queueInputBuffer(videoEncoderInputBufferIndex, 0, 0, presentationTime, 0);
  25.             videoGeneratedIndex++;
  26.         }
  27.  
  28.         // Check if the video encoder has generated any output.
  29.         int videoEncoderOutputBufferIndex = videoEncoder.dequeueOutputBuffer(videoBufferInfo, 0);
  30.         if (videoEncoderOutputBufferIndex >= 0) {
  31.             // Write the output to the file.
  32.             ByteBuffer videoEncoderOutputBuffer = videoEncoderOutputBuffers[videoEncoderOutputBufferIndex];
  33.             videoEncoderOutputBuffer.position(videoBufferInfo.offset);
  34.             videoEncoderOutputBuffer.limit(videoBufferInfo.offset + videoBufferInfo.size);
  35.             mediaMuxer.writeSampleData(videoTrackIndex, videoEncoderOutputBuffer, videoBufferInfo);
  36.  
  37.             // Release the output buffer.
  38.             videoEncoder.releaseOutputBuffer(videoEncoderOutputBufferIndex, false);
  39.         }
  40.  
  41.         // Check if the video encoder has finished encoding the video.
  42.         if ((videoBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
  43.             videoEncoderDone = true;
  44.         }
  45.     }
  46.  
  47.     // Stop and release the video encoder.
  48.     videoEncoder.stop();
  49.     videoEncoder.release();
  50.  
  51.     // Stop and release the MediaMuxer.
  52.     mediaMuxer.stop();
  53.     mediaMuxer.release();
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement