Platforms to show: All Mac Windows Linux Cross-Platform
/AVFoundation/Merge Audio and Video
Required plugins for this example: MBS AVFoundation Plugin, MBS MacCG Plugin, MBS MacCF Plugin, MBS Main Plugin, MBS MacBase Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /AVFoundation/Merge Audio and Video
This example is the version from Sat, 3rd May 2019.
Project "Merge Audio and Video.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
const kCMPersistentTrackID_Invalid = 0
AVFoundation = new myAVFoundationMBS
dim videoFile as FolderItem = SpecialFolder.Desktop.Child("video.m4v")
dim audioFile as FolderItem = SpecialFolder.Desktop.Child("audio.m4a")
if not videoFile.Exists or not audioFile.Exists then
break
Return
end if
//Create AVMutableComposition Object which will hold our multiple AVMutableCompositionTrack or we can say it will hold our video and audio files.
dim mixComposition as AVMutableCompositionMBS = AVMutableCompositionMBS.composition
//Now first load your audio file using AVURLAsset. Make sure you give the correct path of your videos.
dim audioAsset as new AVURLAssetMBS(audioFile)
dim audio_timeRange as CMTimeRangeMBS = CMTimeRangeMBS.Make(CMTimeMBS.kCMTimeZero, audioAsset.duration)
//Now we are creating the first AVMutableCompositionTrack containing our audio and add it to our AVMutableComposition object.
dim b_compositionAudioTrack as AVMutableCompositionTrackMBS = mixComposition.addMutableTrackWithMediaType(AVFoundationMBS.AVMediaTypeAudio, kCMPersistentTrackID_Invalid)
dim audiotracks() as AVAssetTrackMBS = audioAsset.tracksWithMediaType(AVFoundation.AVMediaTypeAudio)
dim error as NSErrorMBS
call b_compositionAudioTrack.insertTimeRange(audio_timeRange, audiotracks(0), CMTimeMBS.kCMTimeZero, error)
//Now we will load video file.
dim videoAsset as new AVURLAssetMBS(videoFile)
dim video_timeRange as CMTimeRangeMBS = CMTimeRangeMBS.Make(CMTimeMBS.kCMTimeZero,audioAsset.duration)
//Now we are creating the second AVMutableCompositionTrack containing our video and add it to our AVMutableComposition object.
dim a_compositionVideoTrack as AVMutableCompositionTrackMBS = mixComposition.addMutableTrackWithMediaType(AVFoundationMBS.AVMediaTypeVideo, kCMPersistentTrackID_Invalid)
dim videotracks() as AVAssetTrackMBS = videoAsset.tracksWithMediaType(AVFoundation.AVMediaTypeVideo)
call a_compositionVideoTrack.insertTimeRange(video_timeRange, videotracks(0), CMTimeMBS.kCMTimeZero, error)
//decide the path where you want to store the final video created with audio and video merge.
dim outputFile as FolderItem = SpecialFolder.Desktop.Child("final.mov")
//Now create an AVAssetExportSession object that will save your final video at specified path.
dim assetExport as new AVAssetExportSessionMBS(mixComposition, AVAssetExportSessionMBS.AVAssetExportPresetPassthrough)
assetExport.outputFileType = "com.apple.quicktime-movie"
assetExport.outputFile = outputFile
assetExport.exportAsynchronously
End EventHandler
Property AVFoundation As myAVFoundationMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Class myAVFoundationMBS Inherits AVFoundationMBS
EventHandler Sub exportAsynchronouslyCompleted(ExportSession as AVAssetExportSessionMBS, tag as variant)
MsgBox "exported."
End EventHandler
End Class
End Project
See also:
The items on this page are in the following plugins: MBS AVFoundation Plugin.