How to get Absolute path for a video using ALAssets Library? - iphone

Its simple, How can i get an absolute path for a video retrieved using ALAssets Library? I want to upload this video as file attachment using ASI framework.
It works if i use UIImagePickerViewController. But thats NOT an option any more. I also dont want to use NSData or copy-pasting video to temp file. Need a smart solution? Anybody ?

You can't get the absolute path (file system path) for a video you retrieved using the AssetsLibrary-Framework. The simple reason for that is that these files are outside your app's sandbox and you can't access them directly. You need to use the getBytes-Method of ALAssetsRepresentation to get the byte data of the video file. Most likely you will have to save this data to a temp file, before you can attach it to an email.
Cheers,
Hendrik

You can use ALAssetPropertyAssetURL. From documentation:
The key to retrieve a URL identifier for the asset.
The corresponding value is an NSURL object.
This URL is used by the library-change notifications to identify assets and asset groups. Only the ALAssetRepresentation and ALAssetsGroup classes support this property.
So, once you get ALAsset object you can get url like this:
asset.defaultRepresentation.url

Related

How to compress video after getting result from PHPickerViewController xcode swift

I know that UIImagePickerController allows you to compress media content upon selection. However, I'm not sure how to do the same with PHPickerViewController. How can I compress a video after getting a result (didFinishPicking), (so I can reduce the amount of time to upload and download the data from then on)
I don't think PHPickerViewController does that, but there are other ways to do it depending on how you work with the picker:
If you're using the default picker configuration (i.e. creating a PHPickerConfiguration without passing the photo library and therefore not having the need to request permissions), you'll have to work with NSItemProvider. From what I learned, the only option might be to save the video to a temporary file using the item provider and the convert it to a lower resolution/bitrate using AVAssetExportSession (see some examples here: How can I reduce the file size of a video created with UIImagePickerController?).
If you are using the picker with an explicitly passed photo library, the PHPickerResult will have the assetIdentifier provided to you, and you can use it to get a respective PHAsset. Then, use PHImageManager's requestAVAsset method to get a video asset of a desired quality. You'll still need to export the AVAsset via AVAssetExportSession though, but with this approach you won't have a (potentially large) full-res temporary file on disk. I was recently working on a open source app that uses this technique, you can find some examples here: https://github.com/vadimbelyaev/TbilisiCleanups/blob/main/TbilisiCleanups/Services/MediaUploadService.swift

Playlist file(m3u8) give another playlist file infinitely not giving any media file

I have a playlist file which give me another playlist file which also give me another playlist file, continously.
How can I play this playlist file? and Where can I found the source of video?
For example, I have a playlist file. That is
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=355670
http://slive.ytn.co.kr:1935/live/ylive_0624_1.sdp/playlist.m3u8?wowzasessionid=195968950
If I access to http://slive.ytn.co.kr:1935/live/ylive_0624_1.sdp/playlist.m3u8?wowzasessionid=195968950, it give me other playlist files.
#EXTM3U
#EXT-X-ALLOW-CACHE:NO
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:894
#EXTINF:11,
media_894.ts?wowzasessionid=195968950
#EXTINF:10,
media_895.ts?wowzasessionid=195968950
#EXTINF:11,
media_896.ts?wowzasessionid=195968950
If I access to the results, it also give me playlist files.
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=373764
http://slive.ytn.co.kr:1935/live/ylive_0624_1.sdp/playlist.m3u8?wowzasessionid=1093961187
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=382539
http://slive.ytn.co.kr:1935/live/ylive_0624_1.sdp/playlist.m3u8?wowzasessionid=1566364859
etc...
Although I can play the url http://slive.ytn.co.kr:1935/live/ylive_0624_1.sdp/playlist.m3u8?wowzasessionid=195968950 in safari in my iphone, I want to play this url in my own iphone app.
How can I play m3u8 file extension by myself?
EDIT - May be a duplicate of this SO question but if you want to persist the file, you'll need to go the hard way listed below.
Easy way = Embed UIWebView in your app and initialize it with the url of the .m3u8 file. This will open up quicktime which understands m3u8 and it will play it as it downloads.
Hard way = manually create the request/responses for each part of the m3u8 session and then download each .ts file in turn from the playlist file. As you get each "chunk" of the video, write it to a file or memory, then append the next "chunk" after it. I've done this for a h.264 encoded .m4v file served as a m3u8 and it worked (my code is too ugly to paste) but the pseudocode is:
Fetch the m3u8, saving any cookies sent and any headers that may be important*
Parse the .m3u8 (the first file, with the #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=373764) and pull our the urls.
Fetch the playlist from one of the URLs in the .m3u8 file (being sure to keep any headers/cookies in the request that may be necessary)
Parse the playlist file, saving as much metadata as you need from the top part of the file, construct an absolute URL for each .ts path, then stuff it into an array.
Iterate over the array (again, being mindful of cookies/headers) and fetch the content of each .ts URL into a file.
Play the downloaded file with whichever Media Framework you choose (search SO for how to do this)
If you just need to play the files, go the easy route. If you need to persist them, you need to do the m3u8 dance.
*HTTP Scoop is invaluable for seeing how the m3u8 "protocol" works and for making sure you're request/response headers are accurate - http://tuffcode.com/
*I used vanilla NSURLConnection synchronous calls as my code was just a proof of concept, other network frameworks like AFNetworking will make this a lot easier.

Is URL obtained from UIImagePickerControllerReferenceURL stable?

I get the Asset Library URL for a photo using the UIImagePickerControllerReferenceURL key on the info dictionary obtained from UIImagePickerController. The url looks like this:
assets-library://asset/asset.JPG?id=1000000002&ext=JPG
Is this URL stable? Can I expect to save it in a document and then at a later date fetch the photo using this URL?
I basically have a iOS photo editing app where I allow the user to do some editing on a bunch of photos. I allow the user to save the project and edit at a later date. I would save these URLs to save the information about which photos are used in the project. I don't want to copy and save all the photos used in a project since there can be many photos used in a project and this will result in unnecessary duplication.
Can you also suggest if there is a better way around.
Asset URLs are supposed to be stable but the format changed between iOS3 and iOS4 and the format in iOS5 is something like '/asset/FA8F1400-82A5-424F-A98C-2883754FA54D.jpg'. You shouldn't store asset URLs and expect them to remain valid forever, which is a shame because that is exactly what you would expect.
You could protect yourself by storing the asset's date and maybe location if it is set. Then if your app needs to, it can go through the asset library and patch up the references if the URL format changes again.

iPhone: To store photo album image URL's and compare later:

As per my query from the below link, i've implemented retrieving photo album images from ALAsset Library.
Upload ALAsset URL images to the server
I get the images Asset URL's like below,
"assets-library://asset/asset.JPG?id=1000000001&ext=JPG"
"assets-library://asset/asset.JPG?id=1000000002&ext=JPG"
"assets-library://asset/asset.JPG?id=1000000003&ext=JPG"
I want to store these in a database (or) file [I don't know which is suitable for comparison for later purposes]. Next time(say,after 30 mins), i want to get again all the image URL's from Asset and compare with the previous image URL's which has stored in a DB, to find if any new images has been added in photo album or not. If any new image is been added in photo album, then i get that image in my code.
I want to know, what should i use to store the image URL strings, whether SQLite database (or) file, and next time which one should be sufficient to compare the previous data and the current data? I thought of going with creating sqlite database and store the data and compare the image URL strings with the stored data like that …. Am i thinking right to go? If yes, may i know how to store the data(image URL strings) in a sqlite database and do comparison between new image URL's and stored data?
I created a sqlite database through Firefox sqlite manager plugin and added a table. I'm not getting any samples further to go through to solve my purposes, i.e. store the image URL strings in sqlite database and do comparison between the stored image URL strings and new image URL strings which i got from Asset library.
Please help and advise.
Thank you!
yes you are going right. Use sqlite database to store URLs follow the tutorial
http://dblog.com.au/iphone-development-tutorials/iphone-sdk-tutorial-reading-data-from-a-sqlite-database/
Edit..sorry, i missed the last comment that explains what you want to do..
Here
http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html
do that tutorial to learn how to set up the database and core data, you'll see how they save information - and from there just change code to suit your needs
Edit again.. after that you can use predicates to pull out info you need, and/or use string comparisons (Google "iPhone sdk compare strings") to compare to a new string.
or create an array of existing URL's and do something like
if ([myArrayOfURLS containsObject:theNewUrl]) {
[self doSomethingWithMyNewURLIGuess];
}

Using iPhone audio files in own app - copy the audio file - is that possible?

I like the user to pick an audio file from the available list of audio and copy it into my own app. In the retrieved items of userMediaItemCollection in the Apple sample code "AddMusic" there is the property: MPMediaItemPropertyAssetURL. But I cant get to it, since I get the error "MPMediaItemPropertyAssetURL undeclared" - although I imported MPMediaItem.h.
In the apple doc it says:
MPMediaItemPropertyAssetURL
A URL pointing to the media item, from which an AVAsset object (or other URL-based AV Foundation object) can be created, with any options as desired. Value is an NSURL object.
The URL has the custom scheme of ipod-library. For example, a URL might look like this:
ipod-library://item/item.m4a?id=12345
Usage of the URL outside of the AV Foundation framework is not supported.
Available in iPhone OS 4.0 and later.
Declared in MPMediaItem.h
Does that mean, it is not possible to access the audio files for own use?
Is there a workaround?
Many thanks
I learned that there is NO way of doing this. As I wrote in the comment - just use MPMediaItemPropertyPersistentID.....
This is an old thread but you can copy a MPMediaItem. You need to use AVAssetExportSession though and not NSFileManager
You should be able to use AVURLAsset, and pass in that URL to the initializer.