如何在Android上确定视频的宽度和高度

发布于 2024-12-31 23:10:54 字数 327 浏览 2 评论 0原文

我有一个视频文件,我想获取视频的宽度和高度。我不想玩它,只是为了获得尺寸。 我尝试使用 MediaPlayer:

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(uriString);
mp.prepare();
int width = mp.getVideoWidth();
int height = mp.getVideoHeight();

但它返回 0 的宽度和高度,因为 VideoSizeChangedEvent 尚未触发。 如何获取视频的宽度和高度?

UPD:我需要 API 版本 7

I have a video file and I want to get width and height of video. I don't want to play it, just to get size.
I've tried to use MediaPlayer:

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(uriString);
mp.prepare();
int width = mp.getVideoWidth();
int height = mp.getVideoHeight();

but it returns 0 for width and height, because VideoSizeChangedEvent didn't fire yet.
How can I get width and height of video?

UPD: I need API version 7

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

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

发布评论

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

评论(8

只为一人 2025-01-07 23:10:54

这适用于 API 级别 10 及更高版本:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource("/path/to/video.mp4");
int width = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int height = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
retriever.release();

This works in API level 10 and up:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource("/path/to/video.mp4");
int width = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int height = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
retriever.release();
开始看清了 2025-01-07 23:10:54

在某些情况下,我们无法读取元数据。为了确保我们获得宽度和高度,最好使用MediaMetadataRetriever创建一个Bitmap,然后从创建的Bitmap中获取宽度和高度>,如下图:

public int getVideoWidthOrHeight(File file, String widthOrHeight) {
    MediaMetadataRetriever retriever = null;
    Bitmap bmp = null;
    FileInputStream inputStream = null;
    int mWidthHeight = 0;                                                                                                                
    try {
        retriever = new  MediaMetadataRetriever();
        inputStream = new FileInputStream(file.getAbsolutePath());
        retriever.setDataSource(inputStream.getFD());
        bmp = retriever.getFrameAtTime();
        if (widthOrHeight.equals("width")){
            mWidthHeight = bmp.getWidth();
        }else {
            mWidthHeight = bmp.getHeight();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally{
       if (retriever != null){
           retriever.release()
       }if (inputStream != null){
           inputStream.close()
       }
    }  
    return mWidthHeight;
}

可以这样调用上面的方法:

// Get the video width
int mVideoWidth = getVideoWidthOrHeight(someFile, "width");
// Get the video height
int mVideoHeight = getVideoWidthOrHeight(someFile, "height");

In some cases, we are unable to read the metadata. To ensure that we get the width and height, it is best to create a Bitmap using MediaMetadataRetriever, then get the width and height from the created Bitmap, as shown below:

public int getVideoWidthOrHeight(File file, String widthOrHeight) {
    MediaMetadataRetriever retriever = null;
    Bitmap bmp = null;
    FileInputStream inputStream = null;
    int mWidthHeight = 0;                                                                                                                
    try {
        retriever = new  MediaMetadataRetriever();
        inputStream = new FileInputStream(file.getAbsolutePath());
        retriever.setDataSource(inputStream.getFD());
        bmp = retriever.getFrameAtTime();
        if (widthOrHeight.equals("width")){
            mWidthHeight = bmp.getWidth();
        }else {
            mWidthHeight = bmp.getHeight();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally{
       if (retriever != null){
           retriever.release()
       }if (inputStream != null){
           inputStream.close()
       }
    }  
    return mWidthHeight;
}

You can call the above method like this:

// Get the video width
int mVideoWidth = getVideoWidthOrHeight(someFile, "width");
// Get the video height
int mVideoHeight = getVideoWidthOrHeight(someFile, "height");
画尸师 2025-01-07 23:10:54

这对我有用

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer mp) {
                int width = mp.getVideoWidth();
                int height = mp.getVideoHeight();
            }
        });

This worked for me

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer mp) {
                int width = mp.getVideoWidth();
                int height = mp.getVideoHeight();
            }
        });
ヤ经典坏疍 2025-01-07 23:10:54

API 级别 7 解决方案:

// get video dimensions
MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource(filename);
            mp.prepare();
            mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                    int orient = -1;

                    if(width < height)
                        orient = 1;
                    else
                        orient = 0;

                }
            });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

API level 7 solution:

// get video dimensions
MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource(filename);
            mp.prepare();
            mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                    int orient = -1;

                    if(width < height)
                        orient = 1;
                    else
                        orient = 0;

                }
            });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
落叶缤纷 2025-01-07 23:10:54

这个实用程序集用于在 Android 中使用 Size 抽象。

它包含一个类 SizeFromVideoFile.java
你可以这样使用它:

ISize size = new SizeFromVideoFile(videoFilePath);
size.width();
size.hight();

This set of utilities to work with the Size abstraction in Android.

It contains an class SizeFromVideoFile.java
You can use it like this:

ISize size = new SizeFromVideoFile(videoFilePath);
size.width();
size.hight();
貪欢 2025-01-07 23:10:54
private static final Uri MOVIE_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
private static final String[] MOVIE_PJT = new String[] {
        MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA,
        MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DATE_TAKEN,
        MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.DURATION,
        MediaStore.Video.Media.SIZE, MediaStore.Video.Media.RESOLUTION };

尝试从系统内容提供商查询。它将返回视频(本地文件)的一些有用信息

private static final Uri MOVIE_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
private static final String[] MOVIE_PJT = new String[] {
        MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA,
        MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DATE_TAKEN,
        MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.DURATION,
        MediaStore.Video.Media.SIZE, MediaStore.Video.Media.RESOLUTION };

try this to query from system content provider. It will return some useful info of the Videos(local files)

夏雨凉 2025-01-07 23:10:54

Kotlin 版本

Kotlin Version ????

data class VideoSize(val width: Int, val height: Int)

Extention Function

fun Uri.getVideoSize(): VideoSize {
        val retriever = MediaMetadataRetriever()
        retriever.setDataSource(appContext, this)
        val width =
            retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt() ?: 0
        val height =
            retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toInt() ?: 0
        retriever.release()
        return VideoSize(width, height)
    }
去了角落 2025-01-07 23:10:54

我的应用程序中使用的 kotlin 版本引用了 user493244 的答案

     /**
     * determine video width and height
     *
     *
     * Refer to [stackoverflow](https://stackoverflow.com/questions/9077436/how-to-determine-video-width-and-height-on-android)
     * answer of  [user493244](https://stackoverflow.com/users/493244/user493244)
     *
     * @param file string with file pathname to use
     * @param widthOrHeight string with size required (width or height)
     * @return mWidthHeight int with video width or height
     */
    fun getVideoWidthOrHeight(file: String?, widthOrHeight: String): Int {
        var retriever: MediaMetadataRetriever? = null
        val bmp: Bitmap?
        //
        var mWidthHeight = 0
        try {
            retriever = MediaMetadataRetriever()
            //
            retriever.setDataSource(file)
            bmp = retriever.frameAtTime
            mWidthHeight = if (widthOrHeight == getString(R.string.width)) {
                bmp!!.width
            } else {
                bmp!!.height
            }
        } catch (e: RuntimeException) {
            e.printStackTrace()
        } finally {
            if (retriever != null) {
                try {
                    retriever.release()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        return mWidthHeight
    }    

kotlin version used in my app referring to user493244's answer

     /**
     * determine video width and height
     *
     *
     * Refer to [stackoverflow](https://stackoverflow.com/questions/9077436/how-to-determine-video-width-and-height-on-android)
     * answer of  [user493244](https://stackoverflow.com/users/493244/user493244)
     *
     * @param file string with file pathname to use
     * @param widthOrHeight string with size required (width or height)
     * @return mWidthHeight int with video width or height
     */
    fun getVideoWidthOrHeight(file: String?, widthOrHeight: String): Int {
        var retriever: MediaMetadataRetriever? = null
        val bmp: Bitmap?
        //
        var mWidthHeight = 0
        try {
            retriever = MediaMetadataRetriever()
            //
            retriever.setDataSource(file)
            bmp = retriever.frameAtTime
            mWidthHeight = if (widthOrHeight == getString(R.string.width)) {
                bmp!!.width
            } else {
                bmp!!.height
            }
        } catch (e: RuntimeException) {
            e.printStackTrace()
        } finally {
            if (retriever != null) {
                try {
                    retriever.release()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        return mWidthHeight
    }    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文