iOS获取视频文件大小和时长

1 、获取网络链接的视频大小和时长

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];// url:网络视频的连接
    NSArray *arr = [asset tracksWithMediaType:AVMediaTypeVideo];// 项目中是明确媒体类型为视频,其他没试过
    CGSize videoSize =CGSizeZero;

    for (AVAssetTrack *track in arr) {
     if([track.mediaType isEqualToString:AVMediaTypeVideo])
     {
        if (track.totalSampleDataLength >= 1048576) {//1048576bt = 1M  小于1m的显示KB 大于1m显示M
            lable.text = [NSString stringWithFormat:@"%.2lldM",track.totalSampleDataLength/1024/1024];
        } else {
            lable.text = [NSString stringWithFormat:@"%.1lldKB",track.totalSampleDataLength/1024];
        }
           videoSize = track.naturalSize;
        
      }

    }

2 、获取保存在你app沙盒中的视频文件

导入 #import <AVFoundation/AVFoundation.h>

/**
 * @method
 *
 * @brief 根据路径获取视频时长和大小
 * @param path       视频路径
 * @return    字典    @"size"--文件大小   @"duration"--视频时长
 */
- (NSDictionary *)getVideoInfoWithSourcePath:(NSString *)path{
    AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
    CMTime   time = [asset duration];
    int seconds = ceil(time.value/time.timescale);

    NSInteger   fileSize = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil].fileSize;

    return @{@"size" : @(fileSize),
             @"duration" : @(seconds)};
}

3 、获取PHAsset 文件大小

 PHAssetResource *resource = [[PHAssetResource assetResourcesForAsset:asset] firstObject];
    long long size = [[resource valueForKey:@"fileSize"] longLongValue];
和
 PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
    options.version = PHVideoRequestOptionsVersionOriginal;
    [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
        if ([asset isKindOfClass:[AVURLAsset class]]) {
            AVURLAsset* urlAsset = (AVURLAsset*)asset;
            NSNumber *size;
            [urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                 
            });
        } else {
            imageLable.text = [NSString stringWithFormat:@"0KB"];

        }
    }];
### 如何在 UniApp 中获取视频文件信息 #### 创建 Video 上下文对象 为了操作 `<video>` 组件,可以使用 `uni.createVideoContext` 方法来创建 video 上下文对象。该上下文提供了多种方法用于控制查询视频组件的状态。 ```javascript const videoContext = uni.createVideoContext('myVideo'); ``` 其中 `'myVideo'` 是 HTML 中 `<video id="myVideo">` 的 ID 属性值[^1]。 #### 查询视频元数据 通过监听 `bindtimeupdate` 者 `onTimeUpdate` 事件可以在每次播放进度更新时获得当前时间以及其它相关信息: ```html <template> <view> <!-- 视频标签 --> <video id="myVideo" :src="videoSrc" @timeupdate="handleTimeUpdate" objectFit="contain"> </video> <!-- 显示视频信息 --> <text>Duration: {{ duration }}s</text> <text>Current Time: {{ currentTime }}s</text> </view> </template> <script> export default { data() { return { videoSrc: '/path/to/video.mp4', duration: 0, currentTime: 0 }; }, methods: { handleTimeUpdate(event) { this.currentTime = event.detail.currentTime; // 当首次接收到有效的时间戳时设置总时长 if (this.duration === 0 && !isNaN(event.detail.duration)) { this.duration = Math.floor(event.detail.duration); } } } }; </script> ``` 上述代码片段展示了如何利用 Vue.js 数据绑定机制实时显示视频的持续时间当前播放位置。注意这里的 `event.detail.duration` `event.detail.currentTime` 提供了访问视频实际属性的方式。 对于本地存储中的 MP4 文件大小,则可以通过调用原生 API 来实现。例如,在 Android 平台上可通过 JavaScript Bridge 调用 Java 方法读取文件系统上的文件尺寸;而在 iOS 上则可能需要借助 Objective-C/Swift 编写的插件来进行同样的工作。不过这些细节取决于具体的应用场景技术栈的选择[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值