Xamarin Forms Android 视频压缩

发布于 2025-01-15 00:14:16 字数 1111 浏览 0 评论 0原文

我的应用程序的一部分(Xamarin Forms - 跨平台)需要减小视频文件的大小。 iOS 自己会做,但 Android 不会。对于照片,我是这样做的:

public byte[] CompressImage(byte[] imageData, int imageQuality, int imageWidth, int imageHeight)
        {
            int smallToBigWidth = imageWidth / 2;
            int smallToBigHeight = imageHeight / 2;
            Bitmap originalImage = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
            float proportions = (float)imageHeight / (float)originalImage.Height;
            float newWidth = (float)originalImage.Width * proportions;
            originalImage = Bitmap.CreateScaledBitmap(originalImage, (int)newWidth, imageHeight, false);
            using (MemoryStream ms = new MemoryStream())
            {
                originalImage.Compress(Bitmap.CompressFormat.Jpeg, imageQuality, ms);
                return ms.ToArray();
            }
        }

但是如何处理视频,以便当您从图库中选择文件时,您会获得简化版本,而不会将新文件保存在手机上?我使用 MediaPicker 下载文件

var file = await MediaPicker.PickVideoAsync ();

并获取 FileResult 类型的文件,这就是我想要在压缩后实现的类型。有人有什么想法吗?我肯定到处都找过了,但什么也没有。

Part of my application (Xamarin Forms - crossplatform) needs to reduce the size of video files. iOS does it itself, but Android doesn't. For photos I do it like this:

public byte[] CompressImage(byte[] imageData, int imageQuality, int imageWidth, int imageHeight)
        {
            int smallToBigWidth = imageWidth / 2;
            int smallToBigHeight = imageHeight / 2;
            Bitmap originalImage = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
            float proportions = (float)imageHeight / (float)originalImage.Height;
            float newWidth = (float)originalImage.Width * proportions;
            originalImage = Bitmap.CreateScaledBitmap(originalImage, (int)newWidth, imageHeight, false);
            using (MemoryStream ms = new MemoryStream())
            {
                originalImage.Compress(Bitmap.CompressFormat.Jpeg, imageQuality, ms);
                return ms.ToArray();
            }
        }

But how do you do it with a video so that when you select a file from the gallery, you get a reduced version without saving the new file on your phone? I download the file using MediaPicker

var file = await MediaPicker.PickVideoAsync ();

and gets a file of the FileResult type, and this is the type I would like to achieve after compression. Anyone have any ideas? I must have looked everywhere and nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2025-01-22 00:14:16

对于 Android,您可以使用 Transcoderhttps://github.com/natario1/Transcoder

您还可以参考示例:https://github.com/leye0/XamarinAndroidFFmpeg

For Android, you could use Transcoder. https://github.com/natario1/Transcoder

You could also refer to sample: https://github.com/leye0/XamarinAndroidFFmpeg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文