Platforms to show: All Mac Windows Linux Cross-Platform
/AVFoundation/Generate Thumbnails
Required plugins for this example: MBS AVFoundation Plugin, MBS Util 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/Generate Thumbnails
This example is the version from Fri, 26th Dec 2013.
Project "Generate Thumbnails.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub NewDocument()
dim f as FolderItem = SpecialFolder.Desktop.Child("test.m4v")
if f.Exists then
Process f
else
MsgBox "Please drop a movie on this application."
end if
End EventHandler
EventHandler Sub Open()
if AVAssetExportSessionMBS.available = false then
MsgBox "Please run on Mac OS X 10.7 or newer."
quit
end if
Generators = new Dictionary
assets = new Dictionary
files = new Dictionary
avEvents = new MyAVFoundationMBS
End EventHandler
EventHandler Sub OpenDocument(item As FolderItem)
Process item
End EventHandler
Sub Process(f as FolderItem)
dim a as AVAssetMBS = AVAssetMBS.assetWithFile(f)
if a = nil then
MsgBox "Failed to read "+f.Name
Return
end if
// make ten thumbnails starting 10 seconds from start
dim g as new AVAssetImageGeneratorMBS(a)
// build list of times
dim times() as CMTimeMBS
dim duration as CMTimeMBS = a.duration
dim FiveSeconds as Double = duration.Timescale * 5
for i as integer = 0 to 9
dim t as CMTimeMBS = CMTimeMBS.Make(FiveSeconds + duration.Value*i/11, duration.Timescale)
times.Append t
next
// with tag we pass reference numer of the item in Dictionary
tagCounter = tagCounter +1
dim tag as integer = tagCounter
Generators.Value(tag) = g
assets.Value(tag) = a
files.Value(tag) = f
g.generateCGImagesAsynchronouslyForTimes(times, tag)
End Sub
Property Generators As Dictionary
Property assets As Dictionary
Property avEvents As MyAVFoundationMBS
Property files As Dictionary
Property tagCounter As Integer
End Class
Class Window1 Inherits Window
EventHandler Sub DropObject(obj As DragItem, action As Integer)
do
if obj.FolderItemAvailable then
app.Process obj.FolderItem
end if
loop until not obj.NextItem
End EventHandler
EventHandler Sub Open()
me.AcceptFileDrop FileTypes1.Mpeg
me.AcceptFileDrop FileTypes1.Quicktime
me.AcceptFileDrop FileTypes1.Video3gpp
me.AcceptFileDrop FileTypes1.VideoAvi
End EventHandler
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 generateCGImagesAsynchronouslyForTimesCompleted(requestedTime as CMTimeMBS, image as CGImageMBS, actualTime as CMTimeMBS, result as integer, error as NSErrorMBS, tag as integer)
// find our stuff
dim f as FolderItem = app.files.Value(tag)
dim a as AVAssetMBS = app.assets.Value(tag)
dim g as AVAssetImageGeneratorMBS = app.Generators.Value(tag)
Select case result
case AVAssetImageGeneratorMBS.AVAssetImageGeneratorSucceeded
dim name as string = f.NameWithoutExtensionMBS+" thumbnail at "+str(actualTime.Seconds)+"s.jpg"
dim imagefile as FolderItem = f.parent.Child(name)
// compress using CGImageDestinationMBS
dim options as new Dictionary
// 80% quality
options.Value(CGImageDestinationMBS.kCGImageDestinationLossyCompressionQuality) = 0.8
dim d as new CGImageDestinationMBS(imagefile, "public.jpeg", 1)
d.AddImage(image, options)
if d.Finalize then
System.DebugLog "Image saved: "+name
else
MsgBox "Failed to save."
end if
case AVAssetImageGeneratorMBS.AVAssetImageGeneratorCancelled
case AVAssetImageGeneratorMBS.AVAssetImageGeneratorFailed
MsgBox error.localizedDescription
end Select
End EventHandler
End Class
FileTypes1
Filetype video/mpeg
Filetype video/quicktime
Filetype video/avi
Filetype video/3gpp
End FileTypes1
End Project
The items on this page are in the following plugins: MBS AVFoundation Plugin.