[英]Get video metadata, how to detect whether the video is taken from front- or back-camera
我想知道視頻是從前置還是后置攝像頭拍攝的。 作為info
返回零。
let asset: PHAsset!
let manager = PHImageManager.default()
if asset.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
options.isNetworkAccessAllowed = true
manager.requestAVAsset(forVideo: asset, options: options) { (asset, audiomix, info) in
}
令人驚訝的是,您可以將PHImageManager.requestImageDataAndOrientation用於視頻資產。
Xcode 12.5
iPhone SE 2020 (iOS 14.6)
let imageManager = PHImageManager.default()
imageManager.requestImageDataAndOrientation(for: videoPHAsset, options: nil) { (data, string, orientation, info) in
if let data = data, let ciImage = CIImage(data: data) {
print(ciImage.properties)
}
}
將ciImage.properties
序列化為 JSON 后,我得到以下結果。
{"ColorModel":"RGB","PixelHeight":1920,"{Exif}":{"ColorSpace":1,"PixelXDimension":1080,"ExifVersion":[2,2,1],"FlashPixVersion":[1,0],"PixelYDimension":1920,"SceneCaptureType":0,"ComponentsConfiguration":[1,2,3,0]},"ProfileName":"HDTV","DPIHeight":72,"PixelWidth":1080,"{TIFF}":{"YResolution":72,"ResolutionUnit":2,"XResolution":72},"{JFIF}":{"DensityUnit":0,"YDensity":72,"JFIFVersion":[1,0,1],"XDensity":72},"Depth":8,"DPIWidth":72}
{"Depth":8,"{TIFF}":{"YResolution":72,"ResolutionUnit":2,"XResolution":72},"PixelHeight":1334,"{JFIF}":{"DensityUnit":0,"YDensity":72,"JFIFVersion":[1,0,1],"XDensity":72},"ProfileName":"sRGB IEC61966-2.1","PixelWidth":750,"ColorModel":"RGB","DPIHeight":72,"DPIWidth":72,"{Exif}":{"ColorSpace":1,"PixelXDimension":750,"ExifVersion":[2,2,1],"FlashPixVersion":[1,0],"PixelYDimension":1334,"SceneCaptureType":0,"ComponentsConfiguration":[1,2,3,0]}}
{"ColorModel":"RGB","{Exif}":{"PixelXDimension":750,"PixelYDimension":1334,"DateTimeOriginal":"2021:01:21 14:25:56","UserComment":"Screenshot"},"{PNG}":{"InterlaceType":0},"HasAlpha":true,"Depth":16,"{TIFF}":{"Orientation":1},"PixelHeight":1334,"ProfileName":"Display P3","PixelWidth":750,"Orientation":1}
{"ExifVersion":[2,3,1],"Flash":24,"LensModel":"iPhone SE (2nd generation) back camera 3.99mm f\\/1.8","OffsetTimeDigitized":"+05:30","SubsecTimeOriginal":"630","LensSpecification":[3.990000009536743,3.990000009536743,1.7999999523162842,1.7999999523162842],"ExposureMode":0,"CompositeImage":2,"LensMake":"Apple","FNumber":1.8,"OffsetTimeOriginal":"+05:30","PixelYDimension":3024,"ApertureValue":1.6959938128383605,"ExposureBiasValue":0,"MeteringMode":5,"ISOSpeedRatings":[400],"ShutterSpeedValue":4.6443251405083465,"SceneCaptureType":0,"FocalLength":3.99,"DateTimeOriginal":"2021:01:21 20:47:05","SceneType":1,"FlashPixVersion":[1,0],"ColorSpace":65535,"SubjectArea":[2013,1511,2217,1330],"PixelXDimension":4032,"FocalLenIn35mmFilm":28,"SubsecTimeDigitized":"630","OffsetTime":"+05:30","SensingMethod":2,"BrightnessValue":0.06030004492448258,"DateTimeDigitized":"2021:01:21 20:47:05","ComponentsConfiguration":[1,2,3,0],"WhiteBalance":0,"ExposureTime":0.04,"ExposureProgram":2}
視頻有"ExifVersion":[2,2,1]
而照片有"ExifVersion":[2,3,1]
。 視頻的 exif 根本不提供有關相機的任何有用信息 - 而照片的 exif 提供。 所有的視頻和照片都是在同一部手機上拍攝的。
在這一點上,如果這些信息甚至被編碼到視頻幀中,仍然毫無頭緒。
這可能對你有幫助。 如果您通過 PHAssetCollectionSubtype.smartAlbumSelfPortraits 過濾獲得 PHAssets,這些資產似乎是用前置攝像頭拍攝的視頻。
let collection = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum,
subtype: PHAssetCollectionSubtype.smartAlbumSelfPortraits,
options: nil)
盡管該視頻沒有人臉,但它目前在 iOS 上被歸類為自拍。 我用 iPhone 12 mini/ iOS15 測試過。
蘋果也這樣說:
一個智能相冊,將使用設備的前置攝像頭拍攝的所有照片和視頻分組。
塊引用
https://developer.apple.com/documentation/photokit/phassetcollectionsubtype/smartalbumselfportraits
所以也許你可以預取自拍相冊資產,然后檢查相冊中是否包含資產以檢測它是由前置攝像頭拍攝的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.