I have AVUrlAsset something like this "file:///var/mobile/Media/DCIM/101APPLE/IMG_1006.MOV". How I can access video from this url in Swift ?
Asked
Active
Viewed 3,906 times
2 Answers
2
This way you can fetch all videos
DispatchQueue.global(qos: .background).async {
PHPhotoLibrary.requestAuthorization { (status) -> Void in
let allVidOptions = PHFetchOptions()
allVidOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.hashValue) //Any type you want to fetch
allVidOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
//Now the set the option to `fetchAssets`
let allVids = PHAsset.fetchAssets(with: allVidOptions)
print("All Videos Count \(allVids.count)")
for index in 0..<allVids.count {
let videoRequestOptions = PHVideoRequestOptions()
videoRequestOptions.deliveryMode = .fastFormat //quality-> 360p mp4
videoRequestOptions.version = .original
PHImageManager.default().requestPlayerItem(forVideo: allVids[index], options: videoRequestOptions , resultHandler: { (playerItem, result) in
// print(result as! [String: AnyObject])
// print(playerItem)
let currentVideoUrlAsset = playerItem?.asset as? AVURLAsset
let currentVideoFilePAth = currentVideoUrlAsset!.url
let lastObject = currentVideoFilePAth.pathExtension
print(lastObject)
if lastObject == "M4V" {
self.arrOfVideos.append(playerItem!)
}
//NSString *lastPath = [videoURL lastPathComponent];
//NSString *fileExtension = [lastPath pathExtension];
//NSLog(@"File extension %@",fileExtension);
var i = Int()
print("Appending.... \(i);)")
i += 1
print("My Videos Count \(self.arrOfVideos.count)")
DispatchQueue.main.async(execute: {
self.tblVideos.reloadData()
})
})
//fetch Asset here
//print(allVids[index].description)
// print(self.arrOfVideos) //will show empty first then after above completion the execution will come here again
}
}
}

Anurag Sharma
- 4,276
- 2
- 28
- 44
-
you have `AVUrlAsset ` ? – Anurag Sharma Jun 02 '17 at 09:42
-
1lets say I just have url (file:///var/mobile/Media/DCIM/101APPLE/IMG_1006.MOV) thats it, so how I can access media from that ? iOS is not allowing direct access of that file path right – Ganesh Jun 02 '17 at 09:44
-
you can access through `PHImageManager` and you will get `playerItem` from it. – Anurag Sharma Jun 02 '17 at 09:46
-
i have updated my answer have a look! this is what you are looking for i think – Anurag Sharma Jun 02 '17 at 09:47
-
@Ganesh Please let me know if you found any difficulty! – Anurag Sharma Jun 02 '17 at 10:28
-
Yes Anurag Sharma, I got to know you are saving assets in a array. but I didn't get how to access particular video from it's assets url. – Ganesh Jun 02 '17 at 10:30
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145713/discussion-between-ganesh-and-anurag-sharma). – Ganesh Jun 02 '17 at 10:40
2
I think, if you knew url path. then you could do as following:
let urlStr = URL.init(fileURLWithPath:"file:///var/mobile/Media/DCIM/101APPLE/IMG_1006.MOV")
let asset = AVURLAsset.init(url: urlStr)
The apple reference helps

farazBhatti
- 11
- 1
- 8

dengApro
- 3,848
- 2
- 27
- 41
-
But when I try to convert url into data , i.e. let data = NSData(contentsOf: asset.url ) am getting nil here – Ganesh Jun 02 '17 at 10:58
-