I am working on
I checked Swift 3: How to add watermark on video ? AVVideoCompositionCoreAnimationTool iOS 10 issue but after applying Watermark, Video is coming in small area of frame size.
Can anyone help me how can I add the
from Add a Watermark on Video after merging Video and Audio Asset into one in Swift3 iOS
Video
based application in Swift3
. As per my requirement I have to merge Video
and Audio
AVAsset
into one, adjust their volumes separately and save the final Video
in iPhone Device gallery
. This is working fine using below code:func mergeVideoAndMusicWithVolume(assetVideo: AVAsset, assetMusic: AVAsset, startAudioTime: Float64,
volumeVideo: Float, volumeAudio: Float){
//To merging a video and a music and set it a volume
let dirPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let docsDir = dirPaths[0] as String
let composition: AVMutableComposition = AVMutableComposition()
let compositionVideo: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType:
AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
let compositionAudioVideo: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType:
AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
let compositionAudioMusic: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType:
AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
//Add video to the final record
do {
try compositionVideo.insertTimeRange(CMTimeRangeMake(kCMTimeZero, assetVideo.duration),
of: assetVideo.tracks(withMediaType: AVMediaTypeVideo)[0], at: kCMTimeZero)
} catch _ {
}
//Extract audio from the video and the music
let audioMix: AVMutableAudioMix = AVMutableAudioMix()
var audioMixParam: [AVMutableAudioMixInputParameters] = []
let assetVideoTrack: AVAssetTrack = assetVideo.tracks(withMediaType: AVMediaTypeAudio)[0]
let assetMusicTrack: AVAssetTrack = assetMusic.tracks(withMediaType: AVMediaTypeAudio)[0]
let videoParam: AVMutableAudioMixInputParameters = AVMutableAudioMixInputParameters(track: assetVideoTrack)
videoParam.trackID = compositionAudioVideo.trackID
let musicParam: AVMutableAudioMixInputParameters = AVMutableAudioMixInputParameters(track: assetMusicTrack)
musicParam.trackID = compositionAudioMusic.trackID
//Set final volume of the audio record and the music
videoParam.setVolume(volumeVideo, at: kCMTimeZero)
musicParam.setVolume(volumeAudio, at: kCMTimeZero)
//Add setting
audioMixParam.append(musicParam)
audioMixParam.append(videoParam)
//Add audio on final record
//First: the audio of the record and Second: the music
do {
try compositionAudioVideo.insertTimeRange(CMTimeRangeMake(kCMTimeZero,
assetVideo.duration), of: assetVideoTrack, at: kCMTimeZero)
} catch _ {
assertionFailure()
}
do {
try compositionAudioMusic.insertTimeRange(CMTimeRangeMake(CMTimeMake(Int64(startAudioTime * 10000),
10000), assetVideo.duration), of: assetMusicTrack, at: kCMTimeZero)
} catch _ {
assertionFailure()
}
//Add parameter
audioMix.inputParameters = audioMixParam
//Remove the previous temp video if exist
let filemgr = FileManager.default
do {
if filemgr.fileExists(atPath: "\(docsDir)/movie-merge-music.mov") {
try filemgr.removeItem(atPath: "\(docsDir)/movie-merge-music.mov")
} else {
}
} catch _ {
}
//Exporte the final record’
let completeMovie = "\(docsDir)/movie-merge-music.mov"
let completeMovieUrl = NSURL(fileURLWithPath: completeMovie)
let exporter: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName:
AVAssetExportPresetHighestQuality)!
exporter.outputURL = completeMovieUrl as URL
exporter.outputFileType = AVFileTypeMPEG4
exporter.audioMix = audioMix
exporter.exportAsynchronously(completionHandler: {
DispatchQueue.main.async { _ in
self.exportDidFinish(exporter)
}
})
}
func exportDidFinish(_ session: AVAssetExportSession) {
if session.status == AVAssetExportSessionStatus.completed {
let outputURL = session.outputURL
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputURL!)
}) { saved, error in
if saved {
let alertController = UIAlertController(title: "Your video was successfully
saved", message: nil, preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
}
My Problem: Now I have to add a Watermark
image on final Video
before saving to Device gallery
.I checked Swift 3: How to add watermark on video ? AVVideoCompositionCoreAnimationTool iOS 10 issue but after applying Watermark, Video is coming in small area of frame size.
Can anyone help me how can I add the
Watermark
on my Video
?from Add a Watermark on Video after merging Video and Audio Asset into one in Swift3 iOS
No comments:
Post a Comment