Ffmpeg 如何使用Xamarin.MP4Transcoder.Transcoder将视频转换为自定义分辨率
我需要使用Xamarin.MP4Transcoder.Transcoder将视频转换为640*480分辨率。目前有两种可用的分辨率720p格式和960x540格式。在Transcoder类中有一个名为(IMediaFormatStrategy)的Transcoder方法 我可以创建具有MIME类型、宽度和高度的MediaFormat对象 通过下面提到的代码片段:Ffmpeg 如何使用Xamarin.MP4Transcoder.Transcoder将视频转换为自定义分辨率,ffmpeg,xamarin.android,video-processing,transcoding,android-ffmpeg,Ffmpeg,Xamarin.android,Video Processing,Transcoding,Android Ffmpeg,我需要使用Xamarin.MP4Transcoder.Transcoder将视频转换为640*480分辨率。目前有两种可用的分辨率720p格式和960x540格式。在Transcoder类中有一个名为(IMediaFormatStrategy)的Transcoder方法 我可以创建具有MIME类型、宽度和高度的MediaFormat对象 通过下面提到的代码片段: MediaFormat obj=MediaFormat.CreateVideoFormat(“视频/mp4”,480640); 我不
MediaFormat obj=MediaFormat.CreateVideoFormat(“视频/mp4”,480640);
代码>我不得不做类似的事情,幸运的是,我发现了它是如何用Java制作的,然后我不得不将它转换成C
生成的类(用于640x360)如下所示:
public class For640x360Format : Java.Lang.Object, IMediaFormatStrategy
{
public static int AUDIO_BITRATE_AS_IS = -1;
public static int AUDIO_CHANNELS_AS_IS = -1;
static String TAG = "640x360FormatStrategy";
static int LONGER_LENGTH = 640;
static int SHORTER_LENGTH = 360;
static int DEFAULT_VIDEO_BITRATE = 8000 * 1000; // From Nexus 4 Camera in 720p
int mVideoBitrate;
int mAudioBitrate;
int mAudioChannels;
public For640x360Format ()
{
mVideoBitrate = DEFAULT_VIDEO_BITRATE;
mAudioBitrate = AUDIO_BITRATE_AS_IS;
mAudioChannels = AUDIO_CHANNELS_AS_IS;
}
public For640x360Format (int videoBitrate)
{
mVideoBitrate = videoBitrate;
mAudioBitrate = AUDIO_BITRATE_AS_IS;
mAudioChannels = AUDIO_CHANNELS_AS_IS;
}
public For640x360Format (int videoBitrate, int audioBitrate, int audioChannels)
{
mVideoBitrate = videoBitrate;
mAudioBitrate = audioBitrate;
mAudioChannels = audioChannels;
}
public MediaFormat CreateAudioOutputFormat (MediaFormat inputFormat)
{
if (mAudioBitrate == AUDIO_BITRATE_AS_IS || mAudioChannels == AUDIO_CHANNELS_AS_IS) return null;
// Use original sample rate, as resampling is not supported yet.
MediaFormat format = MediaFormat.CreateAudioFormat (MediaFormatExtraConstants.MimetypeAudioAac,
inputFormat.GetInteger (MediaFormat.KeySampleRate),
mAudioChannels);
// this is obsolete: MediaCodecInfo.CodecProfileLevel.AACObjectLC, so using MediaCodecProfileType.Aacobjectlc instead
format.SetInteger (MediaFormat.KeyAacProfile, (int)MediaCodecProfileType.Aacobjectlc);
format.SetInteger (MediaFormat.KeyBitRate, mAudioBitrate);
return format;
}
public MediaFormat CreateVideoOutputFormat (MediaFormat inputFormat)
{
int width = inputFormat.GetInteger (MediaFormat.KeyWidth);
int height = inputFormat.GetInteger (MediaFormat.KeyHeight);
int longer, shorter, outWidth, outHeight;
if (width >= height)
{
longer = width;
shorter = height;
outWidth = LONGER_LENGTH;
outHeight = SHORTER_LENGTH;
}
else
{
shorter = width;
longer = height;
outWidth = SHORTER_LENGTH;
outHeight = LONGER_LENGTH;
}
if (longer * 9 != shorter * 16)
{
throw new OutputFormatUnavailableException ("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")");
}
if (shorter <= SHORTER_LENGTH)
{
#if DEBUG
Console.WriteLine ("This video is less or equal to 720p, pass-through. (" + width + "x" + height + ")");
#endif
return null;
}
MediaFormat format = MediaFormat.CreateVideoFormat ("video/avc", outWidth, outHeight);
format.SetInteger (MediaFormat.KeyBitRate, mVideoBitrate);
format.SetInteger (MediaFormat.KeyFrameRate, 30);
format.SetInteger (MediaFormat.KeyIFrameInterval, 3);
// this is obsolete: MediaCodecInfo.CodecCapabilities.COLORFormatSurface, so using MediaCodecCapabilities.Formatsurface instead
format.SetInteger (MediaFormat.KeyColorFormat, (int)MediaCodecCapabilities.Formatsurface);
return format;
}
}
640x360格式的公共类:Java.Lang.Object,IMediaFormatStrategy
{
公共静态整数音频比特率为=-1;
公共静态int音频信道作为=1;
静态字符串TAG=“640x360FormatStrategy”;
静态整数长度=640;
静态整数长度=360;
static int DEFAULT_VIDEO_BITRATE=8000*1000;//来自720p的Nexus 4摄像头
整数比特率;
内毛重晶石;
内莫迪奥海峡;
640x360格式的公共文件()
{
mVideoBitrate=默认\u视频\u比特率;
mAudioBitrate=音频比特率;
mAudioChannels=音频频道;
}
640x360格式(整数视频比特率)的公共文件
{
mVideoBitrate=视频比特率;
mAudioBitrate=音频比特率;
mAudioChannels=音频频道;
}
640x360格式(整数视频比特率、整数音频比特率、整数音频频道)的公共
{
mVideoBitrate=视频比特率;
mAudioBitrate=音频比特率;
MaudioChannel=音频频道;
}
公共媒体格式CreateAudioOutputFormat(媒体格式输入格式)
{
如果(mAudioBitrate==AUDIO_BITRATE_AS|u IS | mAudioChannels==AUDIO_CHANNELS_AS_IS)返回null;
//使用原始采样率,因为还不支持重新采样。
MediaFormat格式=MediaFormat.CreateAudioFormat(MediaFormatExtraConstants.MimetypeAudioAac,
inputFormat.GetInteger(MediaFormat.KeySampleRate),
毛迪奥通道);
//这是过时的:mediacodeinfo.codecoprofilelevel.AACObjectLC,因此改用mediacodeprofiletype.AACObjectLC
format.SetInteger(MediaFormat.KeyAacProfile,(int)mediacodeprofiletype.Aacobjectlc);
format.SetInteger(MediaFormat.KeyBitRate,mAudioBitrate);
返回格式;
}
公共媒体格式CreateVideoOutputFormat(媒体格式输入格式)
{
int width=inputFormat.GetInteger(MediaFormat.KeyWidth);
int height=inputFormat.GetInteger(MediaFormat.KeyHeight);
内长、短、外宽、外高;
如果(宽度>=高度)
{
更长=宽度;
较短=高度;
外径=更长的长度;
向外高度=较短的长度;
}
其他的
{
较短=宽度;
更长=高度;
外径=较短的长度;
伸出高度=较长的长度;
}
如果(长*9!=短*16)
{
抛出新的OutputFormatUnavailableException(“此视频不是16:9,并且无法转码。(“+width+”x“+height+”));
}
如果(更短),非常感谢!