如何在`MediaStore.ACTION_VIDEO_CAPTURE`中将编解码器设置为H.264?(Android)

huangapple go评论74阅读模式
英文:

How to set codec to H.264 in `MediaStore.ACTION_VIDEO_CAPTURE` ? ( Android )

问题

我想录制一段视频并将其发送到服务器。必须是 H.264 格式的 mp4 文件,这样服务器就不需要转换它,而且可以在网页上显示。

以下是我目前创建 Intent 的方式:

Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}

这样可以工作,但是我不知道如何将编解码器设置为 H.264,或者如何检测默认的编解码器。我能找到一些关于 MediaRecorder 的解决方案,但我想避免使用它,因为它对我的需求来说过于复杂,而且我不想专门为此构建一个视频录制界面。MediaStore.ACTION_VIDEO_CAPTURE 应该很完美,因为用户可以录制/回放视频,取消或重试录制,而无需在我的代码中进行额外的编程(和可能的错误),我只需要在 onActivityResult 中获取视频的 URI。

英文:

I would like to record a video and send it to the server. It has to be H.264 mp4, so the server does not have to convert it, and it can be displayed on the web.

This is how I currently create the Intent:

Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}

It works, but I don't know, how to set the codec to H.264, or how to detect the default codec. I was able to find solutions for MediaRecorder, but I would like to avoid using that, because that is too complex for my needs, and I don't want to build a video recorder gui just for that. The MediaStore.ACTION_VIDEO_CAPTURE would be perfect, because the user can record/replay the video, cancel or retry the recording, without any extra programming (and possible bugs) from my site, I just get back the video uri in onActivityResult.

答案1

得分: 1

很不幸,目前似乎没有一种指定要使用的编解码器的方法。正如您可以从 https://developer.android.com/reference/android/provider/MediaStore#ACTION_VIDEO_CAPTURE 看到的那样,支持的附加参数与输出 Uri、持续时间和大小限制以及简单的质量值(0 表示低质量,1 表示高质量)有关。

这是有意为之的;发送 Intent 将导致启动一个在编译时未知的应用程序,该应用程序可能支持或可能不支持所需的所有功能。而且,由于涉及外部应用程序,需要建立较少的参数值范围,以便更容易实现。

正如您已经意识到的,在 Android 中,操作通常可以通过两种方式进行:一种是“简单”方式,在这种情况下,通过 Intent 请求默认的相机应用程序生成视频;另一种是“强大”方式,通常是通过 API,这种情况下是 MediaRecorder API。

恐怕如果您无法成功使用简单的方式,您就必须选择困难的方式;通常可以在网上找到一些样板代码。

不过,对于您的特定情况,我建议让外部应用程序完成捕获视频文件的重负,然后检查是否使用了 H.264 编解码器(很可能是这样)。如果没有,您可以解决这个问题:根据您的用例,如果用户足够了解,可以尝试在录制应用程序中更改设置,或者您可以通过第三方库在应用程序内部实现某种形式的视频转码。

如果您的用例非常特定,以至于您始终知道将使用哪个相机应用程序(即您是设备的制造商,或者您的用例只允许使用特定型号和定制的 Android 版本的硬件),并且您碰巧知道您的相机应用程序确实支持通过某些自定义的附加值来指定视频编解码器,那么您也可以尝试在 Intent 数据中传递该值。然而,恐怕这在通用智能手机目标上不起作用(即您不知道将处理您的意图的相机应用程序是哪个)。

英文:

Unfortunately there does not seem to be a way of specifying the codec to be used. As you can see from https://developer.android.com/reference/android/provider/MediaStore#ACTION_VIDEO_CAPTURE, the supported EXTRAs are related to output Uri, duration and size limits, and a simple quality value (0 low quality, 1 high quality).

This is intentional; sending an Intent will result in starting an unknown (at compile time) app, that may or may not support all the functionality required. Also, since an external app is involved, the fewer parameter value ranges have to be established the easier it is to implement.

As you already realized, in Android operations can usually be performed two ways: a "simple" one, in this case asking for the default camera app to produce a video, usually by means of an Intent, and a "powerful" one, usually by means of an API, in this case the MediaRecorder API.

I'm afraid that if you are unable to successfully use the easy way you'll have to go the hard way; usually some boilerplate code can be readily found online.

Still though, for your specific case, I'd let the external app do the heavy lifting of capturing a video file, and later check if H.264 is the codec that has been used (it will most likely be, imho). If not you could work around that: depending on your use case, ask the user (if he/she is knowledgeable enough) to try changing settings in the recording app, or you could implement some form of video transcoding inside your app via a third-party library.

If your use case is so specific that you do always know which camera app will be used (i.e. you are the manufacturer of the devices, or your use case only allows for one specific model and custom android version of the hardware), and you happen to know that your camera app does indeed support specifying the video codec via some custom EXTRA value, you can also try passing that in the Intent data. Alas, I'm afraid this will not work with a generic smartphone target (i.e. you do not know the camera app that will happen to handle your intent).

huangapple
  • 本文由 发表于 2020年10月19日 16:52:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64424051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定