Kodeco Forums

How to Play, Record, and Merge Videos in iOS and Swift

Learn the basics of working with videos on iOS with AV Foundation in this tutorial. You'll play, record, and even do some light video editing!


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5135-how-to-play-record-and-merge-videos-in-ios-and-swift

Hey great tutorial.

But is there any way in which I can get didFinishPickingMediaWithInfo called inside the videoHelper only, so that I will not have to expose any other functions or delegates to the ViewController.

That was an AWESOME tutorial! Gave me exactly the code I needed. Thank you!

One thing I want to do is save the location of videos the user records (can’t work out where that info is in your code and how to access it) so that I can write some code where the app itself can call the videos up and edit them into a cool video for the user.

How do I do this?

Please explain like I am 5, I am new to this.

Many thanks,
Don

@owenb Can you please help with this when you get a chance? Thank you - much appreciated! :]

Yeah, one way is to make videoHelper a singleton. Then each VC can grab a shared instance of it and set it to the delegate of mediaUI. But you will still need to pass the VC to videoHelper when you want the picker to appear that way you have a UI context to view it in.

No prob.
The MergeVideoViewController.swift saves the merged videos together into one file. The code starts on line 161:

    // 4 - Get path
    guard let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .long
    dateFormatter.timeStyle = .short
    let date = dateFormatter.string(from: Date())
    let url = documentDirectory.appendingPathComponent("mergeVideo-\(date).mov")

Thank you, much appreciated. Your tutorials are of the highest level.
Respect,
Donovan

Hi Owen! Very nice tutorial :wink:

I just would ask you how to customize appearance on recordViewController, even if is possible to on preview.layer.

Then we need to upload video to our server and we don’t find the custom class to do so.

For that and other little customization maybe we can contact you in private?

Thanks
Mike

@owenb Do you have any feedback regarding this? Thank you - much appreciated! :]

Hi Mike,
Sure, feel free to email me at owen@back-40.com.

Hi! Please can you help. I want to be able to edit with an audio and a video file I placed in the assets of my project. I can reach it as a NSDataAsset, but I can’t edit with it unless I convert it into an AVAsset? Is that right?
How does one do that? Is that the only way?
Thanking you in advance,
Donovan

@owenb Can you please help with this when you get a chance? Thank you - much appreciated! :]

If its in your project, then grab it as a url and create the AVAsset like this:
let avAsset = AVAsset(url: url)

Is it possible to record slow-motion or time-lapse videos using UIImagePickerController?

Hi Scott,
Hmm not that I’m aware of… unless you did your own custom camera handling. But even then it’d take some digging to see how.

Hi there,

First, thank you for this great tutorial !

Second, I’d like to why the sound of my two videos is missing in the finale merge and what is the right way to load my sound but with keeping the sound of my video ? Because I d’ like to achieve a kind of video where people can introduce themselves with a little sound behind their video.

edit : I also noticed a crash in the console about an animation executed in the background thread when merge is done :
[Animation] +[UIView setAnimationsEnabled:] being called from a background thread. Performing any operation from a background thread on UIView or a subclass is not supported and may result in unexpected and insidious behavior

this is because in MergeVideoViewController.swift → func exportDidFinish(_ session: AVAssetExportSession) at saveVideoToPhotos you present an alertController in a background thread

Thanks

@owenb Can you please help with this when you get a chance? Thank you - much appreciated! :]

I found the solution for keeping the audio of the videos + adding a new audio track on it, you have to add a new track of AVMediaType.audio

I used this question/answer (and translate a Swift version of it :wink: ) : iOS AVFoundation Export Session is missing audio - Stack Overflow

Hey mehdi.c, glad you found a solution to keep the audio… And good catch on the UI/background thread issue.

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!