Platforms to show: All Mac Windows Linux Cross-Platform
/AVFoundation/Make Video From Images
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/Make Video From Images
This example is the version from Sat, 18th Oct 2024.
Project "Make Video From Images.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
if AVAssetExportSessionMBS.available = false then
MsgBox "Please run on Mac OS X 10.7 or newer."
quit
end if
End EventHandler
End Class
Class Window1 Inherits Window
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action()
If adaptor.assetWriterInput.isReadyForMoreMediaData = False Then Return
dim g as Graphics = pic.Graphics
dim c as integer = counter*255/pic.height
g.ForeColor = rgb(c, 0, 0)
g.Drawline 0, counter, g.Width, counter
Dim t As CMTimeMBS = CMTimeMBS.Make(600/30*counter, 600) // 30fps in 600 time units per second0
If False Then
// this uses our buffer to reuse it for each frame
buffer.Flush
Call adaptor.appendPixelBuffer(buffer, t)
Else
// this copies the picture into a new buffer
Call adaptor.appendPicture(pic, t)
end if
counter = counter + 1
if counter = g.Height then
// finish
me.mode = 0
videoWriterInput.markAsFinished
dim endtime as CMTimeMBS = CMTimeMBS.Make(600/30*counter, 600) // 30fps in 600 time units per second0
videoWriter.endSessionAtSourceTime endtime
If Not videoWriter.finishWriting Then
MsgBox "Failed to finish writing file."
'break
return
End If
adaptor = nil
videoWriterInput = nil
videoWriter = nil
// done
beep
file.Launch
end if
Backdrop = pic
End EventHandler
End Control
EventHandler Sub Open()
AVFoundation = New MyAVFoundationMBS
pic = new Picture(Width, Height, 32)
buffer = new CVPixelBufferMBS(pic)
file = SpecialFolder.Desktop.Child("test.m4v")
if file.Exists then file.Delete
dim error as NSErrorMBS
videoWriter = New AVAssetWriterMBS(file, AVFoundation.AVFileTypeMPEG4, error)
if videoWriter = nil then
MsgBox "Failed to create video writer!"
break
Return
end if
dim videoSettings as new Dictionary
videoSettings.Value(AVFoundation.AVVideoCodecKey) = AVFoundation.AVVideoCodecH264
videoSettings.Value(AVFoundation.AVVideoWidthKey) = Width
videoSettings.Value(AVFoundation.AVVideoHeightKey) = height
videoWriterInput = AVAssetWriterInputMBS.assetWriterInputWithMediaType(AVFoundation.AVMediaTypeVideo, videoSettings)
if videoWriterInput = nil then
MsgBox "Failed to create video writer input!"
break
Return
end if
dim options as new Dictionary
options.Value(CVPixelBufferMBS.kCVPixelBufferWidthKey) = buffer.Width
options.Value(CVPixelBufferMBS.kCVPixelBufferHeightKey) = buffer.Height
options.Value(CVPixelBufferMBS.kCVPixelBufferPixelFormatTypeKey) = buffer.kCVPixelFormatType_32ARGB
adaptor = AVAssetWriterInputPixelBufferAdaptorMBS.assetWriterInputPixelBufferAdaptorWithAssetWriterInput(videoWriterInput, options)
if adaptor = nil then
MsgBox "Failed to create adaptor!"
break
Return
end if
if not videoWriter.canAddInput(videoWriterInput) then
break
end if
// allows to go faster than real time...
videoWriterInput.expectsMediaDataInRealTime = true
videoWriter.addInput videoWriterInput
//Start a session:
if not videoWriter.startWriting then
MsgBox "Failed to start writing."
break
Return
end if
videoWriter.startSessionAtSourceTime CMTimeMBS.kCMTimeZero
if buffer.Handle = 0 then
MsgBox "Failed to create CVPixelBufferMBS."
break
Return
end if
timer1.mode = 2
End EventHandler
Property AVFoundation As MyAVFoundationMBS
Property adaptor As AVAssetWriterInputPixelBufferAdaptorMBS
Property buffer As CVPixelBufferMBS
Property counter As Integer
Property file As FolderItem
Property pic As Picture
Property videoWriter As AVAssetWriterMBS
Property videoWriterInput As AVAssetWriterInputMBS
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
End Class
Sign
End Sign
End Project
See also:
The items on this page are in the following plugins: MBS AVFoundation Plugin.