How to load native album video using swift - ios

I'm an undergraduate student and I'm witring an iPhone HumanSeg app. But now I have a problem, that I have a native video in album, and I need to load that video into my code and do some processing. My codes are below:
let filePath = Bundle.main.path(forResource: "1", ofType: "mp4")
let videoURL = NSURL(fileURLWithPath: filePath!)
let avAsset = AVAsset(url: videoURL as URL)
But when I run this code, Xcode just tells me that filePath is nil. I assert that 1.mp4 is in both Assets.xcaassets and iPhone album. Is there anyone who'd like to offer some help?
By the way, How can I get the images(in UIImage format) in the video at the fastest speed? For each image at given time, I really have to read it in no more than 5ms so I may output the preserved video at a good fps.

Check target's build phases, whether the file is being copied to the bundle.Also the check the box to include the file. This code is correct for fetching that file.

Related

How do I search for multiple file types using Bundle.main.path

At the moment I've got
let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: "flac")
I want to have the audioPath find songs with multiple audio extensions like .mp3, .wav, etc but I can't figure out how to do this?
I looked in the bundle programming guide (https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW7) but still couldn't figure it out.
Thanks heaps
edit: I know the names of the files, but in a situation where the folder has mp3's and flacs and wav files and all those other extensions, I want the code to be able to play all of those types of audio files and not just one.
edit: maybe this bit of code is also relevant where I tell it to find only flac files (also wasn't sure how to extend this to other audio file types):
for song in songPath
{
var mySong = song.absoluteString
if mySong.contains (".flac")

Share video to Instagram on iOS

Is it possible to share a video on Instagram without saving it to the user's Camera Roll?
this is what i tried so far:
let instagramURL = URL(string: "instagram://app")!
if (UIApplication.shared.canOpenURL(instagramURL)) {
self.documentController = UIDocumentInteractionController(url: videoURL)
self.documentController.delegate = self
self.documentController.uti = "com.instagram.exlusivegram"
self.documentController.presentOpenInMenu(from: self.view.frame, in: self.view, animated: true)
} else {
print(" Instagram isn't installed ")
}
videoURL is the URL of the video i saves in the Documents folder.
if I save the URL with Instagram.igo at the end, then when i choose Instagram to share it opens like this:
if I save the video with .mov at the end, it seems that Instagram share opens with a photo (like video thumbnail) and not a video.
What exactly you did wrong is difficult to determine, but I'll try to address where you might have been wrong.
First of all make sure that the videoURL is defined locally. When I wrote an app posting to Instagram, I first defined the path with file manager, like:
let tempDirectory = FileManager().temporaryDirectory
var postingPath = tempDirectory.appendingPathComponent("postingVideo")
postingPath = postingPath.appendingPathExtension("igo")
I then wrote the movie into this directory and almost have the same code as you.
Do take into consideration that you need to create the igo folder and then save the movie into this folder. If you try to refer to the folder you have the movie in, your app will not read the movie file and nothing will be posted. When it is a movie file from the photo library, use an AVExportSession and export it to the postingPath.
self.instagramController = UIDocumentInteractionController.init(url: postingPath)
self.instagramController.uti = "com.instagram.exclusivegram"
self.instagramController.delegate = self
self.instagramController.presentOpenInMenu(from:
self.navigationItem.rightBarButtonItem!, animated: true)
However, you do need to make your class posting to instagram adhere to the UIDocumentInteractionControllerDelegate, otherwise you need cast self. You also need to include Social. I don't think you have done anything wrong in the second part of this post, because you would have gotten xcode errors and warnings.
I hope the first part can help you further, since I don't think the second part has given you problems.

Playing Video from Online URL works but from Local Path doesn't

I am having trouble playing a local video on my computer. I used a library called Player which is quite straight forward, and in the example project, it's using a Vine video with supplying a link at the top, and it's working:
let videoUrl = NSURL(string: "https://v.cdn.vine.co/r/videos/AA3C120C521177175800441692160_38f2cbd1ffb.1.5.13763579289575020226.mp4")!
Thus, in ViewDidLoad, self.player.setUrl(videoUrl) works.
I tried to download the Vine video to my local. I saved it as aaa.mp4.
I dragged the mp4 file in xCode project.
I added the file in Copy Bundle Resources
Then I used;
let path = NSBundle.mainBundle().pathForResource("aaa", ofType:"mp4")!
let videoUrl = NSURL(string: path)!
viewDidLoad() {
// print(path)
// print(videoUrl)
self.player.setUrl(videoUrl)
}
The video doesn't get played, but
print(path) - gives me:
/Users/sentiasa/Library/Developer/CoreSimulator/Devices/67160785-1BDB-4046-BD58-A8C448938A4F/data/Containers/Bundle/Application/5456FF89-904F-4AE6-A90C-747B6A371745/Player.app/aaa.mp4
and print(videoUrl) - gives me:
/Users/sentiasa/Library/Developer/CoreSimulator/Devices/67160785-1BDB-4046-BD58-A8C448938A4F/data/Containers/Bundle/Appl ... /aaa.mp4
Please note that, if I give the path a filename that doesn't exists, it throws an error, thus I know the file is there (and exists) in my case.
What may be the problem? What am I missing? I don't receive any warnings or errors
Use
NSURL(fileURLWithPath: path)!
instead.

What's the path to save a video in iPad?

I'm saving a video on my iPad with this code on swift:
let paths = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as String
var filePath:String? = nil
var fileNamePostfix = 0
do {
filePath =
"\(documentsDirectory)/\(dateTimePrefix)-\(fileNamePostfix++).mp4"
} while (NSFileManager.defaultManager().fileExistsAtPath(filePath))
let fileUrl = NSURL(fileURLWithPath: filePath)
self.fileOutput.startRecordingToOutputFileURL( fileUrl , recordingDelegate: delegate)
But I can't see if my video is saving because I can't open path var/mobile/media...
There are any form to save pictures on photos folder?
Thanks!!
You can't save assets to the Media Library — the stuff that appears in the Music, Videos, and Podcasts apps. The only way to get things into there is by syncing from iTunes on the desktop or downloading from the iTunes Store.
If you want to save a video so that it appears in the Photos app, use the Photos framework in iOS 8:
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let request = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(url)
}, completionHandler: { success, error in
if !success { NSLog("Failed to create video: %#", error) }
})
(In iOS 7 and earlier, use the AssetsLibrary framework instead.)
If you just want to see if the files are getting into the documents folder on your device then you can see the container in the Devices window of Xcode 6.
Just Window > Devices then select your device. Then select your app, click the gear button and tell it if you want to view, download, or replace the container for the app.
Note that you can also save the videos into the Saved Photos album, assuming they are in the right format. Check out this method in the ALAssetsLibrary Class:
writeVideoAtPathToSavedPhotosAlbum:completionBlock:
You can't access the photo system folder directly. However if you just want to see the files you've saved, you don't have to. Just enable document sharing by adding UIFileSharingEnabled to your Info.plist.
When you plug your device in and open iTunes, it will show your documents folder like so:
More info on filesharing here.

Convert GIF to MP4 on device

Is it possible to take a remote (but I can download it first if needed) GIF sequence and make a MPMoviePlayerViewController playable mp4 on the device?
I have tried using http://api.online-convert.com/, but the API doesn't suit and the free version is too restricted for our needs.
Imagemagick for iOS also doesn't seem to include GIF support.
Swift 3.0 version.
https://gist.github.com/powhu/00acd9d34fa8d61d2ddf5652f19cafcf
Usage
let data = try! Data(contentsOf: Bundle.main.url(forResource: "gif", withExtension: "gif")!)
let tempUrl = URL(fileURLWithPath:NSTemporaryDirectory()).appendingPathComponent("temp.mp4")
GIF2MP4(data: data)?.convertAndExport(to: tempUrl, completion: { })
This wasn't so easy. There is a gist snippet here with the solution.
1st - Separate all images from GIF .
2nd - Create video from that separate images.
Please check this one. i think you can solution which you want.
With this Link you can easily convert GIF file to MP4.
Click
https://github.com/dennygajera/gifToMp4

Resources