【HarmonyOS NEXT】VideoDecoder相关的API从哪个版本开始支持

【关键字】

VideoDecoder / 视频解码 / API级别 / 支持版本 / API10

【问题描述】

VideoDecoder相关的API从哪个版本开始支持?

【解决方案】

  1. API9开始即支持VideoDecoder相关的API,但功能可能不完善,不推荐使用。

    参考文档:https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/_video_decoder-0000001446810536-V3

  2. 为了保证SDK一致性,建议您使用API10及以上版本进行开发。

    参考文档:

    API10:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V1/_video_decoder-0000001630265929-V1

    API11:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/_video_decoder-0000001774281286

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,演示如何使用MediaCodec和MediaMuxer API剪切视频。请注意,这只是一个基本示例,实际上需要更多的代码来完成这个任务,例如处理不同的视频格式和编解码器参数等。 ```java import android.media.MediaCodec; import android.media.MediaCodecInfo; import android.media.MediaExtractor; import android.media.MediaFormat; import android.media.MediaMuxer; import android.os.Environment; import android.util.Log; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; public class VideoClipper { private static final String TAG = "VideoClipper"; private static final String SAMPLE_PREFIX = "video_clip_"; private static final String SAMPLE_EXTENSION = ".mp4"; private static final int TIMEOUT_US = 10000; public static void clipVideo(String inputVideoPath, long startMs, long endMs) throws IOException { File inputFile = new File(inputVideoPath); MediaExtractor extractor = new MediaExtractor(); extractor.setDataSource(inputFile.toString()); int trackCount = extractor.getTrackCount(); int videoTrackIndex = -1; MediaFormat videoFormat = null; // Find the first video track index and its format for (int i = 0; i < trackCount; i++) { MediaFormat format = extractor.getTrackFormat(i); String mime = format.getString(MediaFormat.KEY_MIME); if (mime.startsWith("video/")) { videoTrackIndex = i; videoFormat = format; break; } } if (videoTrackIndex == -1) { throw new RuntimeException("No video track found in " + inputVideoPath); } // Configure the video codec MediaCodec videoDecoder = MediaCodec.createDecoderByType(videoFormat.getString(MediaFormat.KEY_MIME)); videoDecoder.configure(videoFormat, null, null, 0); videoDecoder.start(); // Configure the video muxer String outputVideoPath = new File(Environment.getExternalStorageDirectory(), SAMPLE_PREFIX + System.currentTimeMillis() + SAMPLE_EXTENSION).getAbsolutePath(); MediaMuxer muxer = new MediaMuxer(outputVideoPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); // Copy the video header ByteBuffer header = ByteBuffer.allocate(1024); videoDecoder.getOutputFormat().getByteBuffer("csd-0").rewind(); header.put(videoDecoder.getOutputFormat().getByteBuffer("csd-0")); header.put(videoDecoder.getOutputFormat().getByteBuffer("csd-1")); header.flip(); int videoTrackIndexOut = muxer.addTrack(videoDecoder.getOutputFormat()); muxer.start(); // Extract video frames and write to muxer extractor.selectTrack(videoTrackIndex); ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); boolean inputDone = false; boolean outputDone = false; boolean videoDone = false; long videoStartTimeMs = -1; long videoEndTimeMs = -1; while (!outputDone) { if (!inputDone) { int inputIndex = videoDecoder.dequeueInputBuffer(TIMEOUT_US); if (inputIndex >= 0) { ByteBuffer inputBuffer = videoDecoder.getInputBuffer(inputIndex); int sampleSize = extractor.readSampleData(inputBuffer, 0); if (sampleSize < 0) { videoDecoder.queueInputBuffer(inputIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM); inputDone = true; } else { long presentationTimeUs = extractor.getSampleTime(); if (videoStartTimeMs == -1) { videoStartTimeMs = presentationTimeUs / 1000; } if (videoEndTimeMs == -1 || presentationTimeUs / 1000 < videoEndTimeMs) { videoDecoder.queueInputBuffer(inputIndex, 0, sampleSize, presentationTimeUs, 0); extractor.advance(); } else { inputDone = true; } } } } if (!videoDone) { MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo(); int outputIndex = videoDecoder.dequeueOutputBuffer(bufferInfo, TIMEOUT_US); if (outputIndex >= 0) { if (bufferInfo.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM) { outputDone = true; } else { ByteBuffer outputBuffer = videoDecoder.getOutputBuffer(outputIndex); buffer.clear(); buffer.put(header); buffer.put(outputBuffer); buffer.flip(); muxer.writeSampleData(videoTrackIndexOut, buffer, bufferInfo); videoDecoder.releaseOutputBuffer(outputIndex, false); if (videoEndTimeMs == -1 && bufferInfo.presentationTimeUs / 1000 >= endMs) { videoEndTimeMs = bufferInfo.presentationTimeUs / 1000; videoDone = true; } } } } if (inputDone && videoDone) { outputDone = true; } } // Release resources extractor.release(); if (videoDecoder != null) { videoDecoder.stop(); videoDecoder.release(); } if (muxer != null) { muxer.stop(); muxer.release(); } Log.i(TAG, "Video clipped and saved to " + outputVideoPath); } } ``` 要使用这个示例代码,只需调用`clipVideo`方法并传入要剪切的视频文件路径、开始时间和结束时间。例如: ```java try { VideoClipper.clipVideo("/sdcard/input.mp4", 10000, 20000); } catch (IOException e) { e.printStackTrace(); } ``` 这将在SD卡根目录下创建一个名为“video_clip_*.mp4”的新视频文件,其中*是当前时间的毫秒级时间戳。这个新文件将是原始视频文件从10秒到20秒的剪辑版本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值