在开发过程中我们或许需要,在文件未打开的情况下读取文件信息。或者在视频加载前读取视频编码格式,以及视频的尺寸等等。以下提供三种Object-c macOS下的实现方法:
NSFileManager 读取文件信息-(void)getFileInfo:(NSString *)path{
NSFileManager *manger = [NSFileManager defaultManager];
NSDictionary *dic= [manger attributesOfItemAtPath:path error:nil];
NSLog(@"%s: \n %@",__func__,dic);
}
AVURLAsset 读取文件信息
-(void)getVideoWithAsset:(NSString *)path
{
NSLog(@"%s",__func__);
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:path] options:nil];
NSArray *metaArray = [asset commonMetadata];
NSArray *assetTrack = [asset tracks];
for ( AVMetadataItem *item in metaArray)
NSLog(@"\n %@",item);
for (AVAssetTrack *track in assetTrack) {
NSLog(@" %f,%f",track.naturalSize.height,track.naturalSize.width);
}
}
系统自带的 mdls 脚本 读取文件信息
-(void)getVideoInfoWithShell:(NSString *)path{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/mdls"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects:path, nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
NSLog (@"%s: \n%@",__func__, string);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)