Get video metadata, how to detect whether the video is taken from front- or back-camera

1k views Asked by At

I want to know whether is video is taken from the front or back camera. As info return nil.

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
}
2

There are 2 answers

0
Tomo On

This may help you. If you get PHAssets with filtering by PHAssetCollectionSubtype.smartAlbumSelfPortraits, those assets seem to videos are taken in front camera.

 let collection = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum,
                                                        subtype: PHAssetCollectionSubtype.smartAlbumSelfPortraits,
                                                        options: nil)

Even though the video doesn't have faces, it is categorized as a selfie on iOS for now. I tested with iPhone 12 mini/ iOS15.

Apple also says like this:

A smart album that groups all photos and videos captured using the device’s front-facing camera.

Blockquote

https://developer.apple.com/documentation/photokit/phassetcollectionsubtype/smartalbumselfportraits

So maybe you can prefetch selfie album assets, then check if an asset is contained in the album to detect it is taken by the front camera.

3
Tarun Tyagi On

Surprisingly you are able to use PHImageManager.requestImageDataAndOrientation for VIDEO assets.

Xcode 12.5
iPhone SE 2020 (iOS 14.6)

Observations

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)
    }
}

Upon serializing ciImage.properties into JSON, I got following results.

1. Video (back camera)
{"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}
2. Video (front camera)
{"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]}}
3. Screenshot
{"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}
4. Photo (has a TON of information and JSON serialization crashes, so I extracted only the "{Exif}" part)
{"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}

The videos have "ExifVersion":[2,2,1] while the photo has "ExifVersion":[2,3,1]. The videos' exif doesn't provide any useful information about camera at all - while photo's exif does. All of the videos & photos were captured on the same phone.

At this point, still clueless if this information is even encoded into video frames at all.