Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Main Plugin, MBS AVFoundation Plugin, MBS Util Plugin, MBS MacCG Plugin, MBS MacCF Plugin, MBS MacBase Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /AVFoundation/Transcode batch
This example is the version from Sat, 19th Jul 2013.
Project "Transcode batch.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 MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control BevelButton1 Inherits BevelButton
ControlInstance BevelButton1 Inherits BevelButton
EventHandler Sub Action()
dim f as FolderItem = GetOpenFolderItem("")
if f<>Nil then
add f
end if
End EventHandler
End Control
Control BevelButton2 Inherits BevelButton
ControlInstance BevelButton2 Inherits BevelButton
EventHandler Sub Action()
dim f as FolderItem = SelectFolder
if f<>Nil then
Addfolder f
end if
End EventHandler
End Control
Control NextFileButton Inherits BevelButton
ControlInstance NextFileButton Inherits BevelButton
EventHandler Sub Action()
me.Enabled = False
NextFile
End EventHandler
End Control
Control DoneTimer Inherits Timer
ControlInstance DoneTimer Inherits Timer
End Control
EventHandler Sub DropObject(obj As DragItem, action As Integer)
do
if obj.FolderItemAvailable then
dim f as FolderItem = obj.FolderItem
if f.Directory then
Addfolder f
else
add f
end if
end if
loop until not obj.NextItem
End EventHandler
EventHandler Sub Open()
AcceptFileDrop FileTypes1.AudioAac
AcceptFileDrop FileTypes1.AudioAiff
AcceptFileDrop FileTypes1.Mp3
AcceptFileDrop FileTypes1.Mpeg
AcceptFileDrop FileTypes1.Quicktime
AcceptFileDrop FileTypes1.SpecialFolder
AcceptFileDrop FileTypes1.AudioXM4a
AcceptFileDrop FileTypes1.AudioXM4b
AcceptFileDrop FileTypes1.AudioXM4p
AVFoundation = new MyAVFoundation
if DebugBuild then
// for debugging I have a folder on desktop with test files...
dim testfolder as FolderItem = SpecialFolder.Desktop.Child("test")
if testfolder.Exists then Addfolder testfolder
end if
End EventHandler
Sub Addfolder(f as FolderItem)
dim c as integer = f.Count
for i as integer = 1 to c
dim file as FolderItem = f.TrueItem(i)
if file<>Nil and file.Visible then
if file.Directory then
Addfolder file
else
add file
end if
end if
next
End Sub
Sub Cleanup()
list.RemoveRow 0
// plugin called markAsFinished already!
if assetWriter<>nil then
call assetWriter.finishWriting
end if
if assetReader<>nil then
assetReader.cancelReading
end if
// release a lot of stuff
assetReader = nil
assetReaderOutput = nil
assetWriter = nil
assetWriterInput = nil
songAsset = nil
End Sub
Sub MoveFileToTrash(f as FolderItem)
f.Delete
dim r as FolderItem
'call MacFileOperationMBS.MoveObjectToTrashSync(f, r, MacFileOperationMBS.kFSFileOperationDefaultOptions)
End Sub
Sub NextFile()
if List.ListCount = 0 then Return
dim file as FolderItem = List.RowTag(0)
List.CellBold(0,0) = true
songAsset = AVURLAssetMBS.URLAssetWithFile(file)
dim assetError as NSErrorMBS
assetReader = AVAssetReaderMBS.assetReaderWithAsset(songAsset, assetError)
if assetError<>nil then
dim e as string = assetError.localizedDescription
MsgBox "Error: "+e
break
Return
end if
dim audioSettings as Dictionary = nil // no settings
dim tracks() as AVAssetTrackMBS = songAsset.tracks
assetReaderOutput = AVAssetReaderAudioMixOutputMBS.assetReaderAudioMixOutputWithAudioTracks(tracks, audioSettings)
if not assetReader.canAddOutput(assetReaderOutput) then
break
MsgBox "can't add reader output... die!"
return
end if
assetReader.addOutput(assetReaderOutput)
exportFile = SpecialFolder.Desktop.Child(file.name+".wav")
if exportFile<>Nil then
MoveFileToTrash exportFile
exportFile = SpecialFolder.Desktop.Child(file.name+".wav")
end if
assetWriter = AVAssetWriterMBS.assetWriterWithFile(exportFile, AVFoundationMBS.AVFileTypeWAVE, assetError)
if assetError<>nil then
dim e as string = assetError.localizedDescription
MsgBox "Error: "+e
break
Return
end if
// Mono layout
dim channelLayout as new QTAudioChannelLayoutMBS
channelLayout.ChannelLayoutTag = QTAudioChannelLayoutMBS.kAudioChannelLayoutTag_Mono
// settings
dim outputSettings as new Dictionary
outputSettings.Value(AVFoundationMBS.AVLinearPCMBitDepthKey) = 16
outputSettings.Value(AVFoundationMBS.AVLinearPCMIsNonInterleaved) = false
outputSettings.Value(AVFoundationMBS.AVLinearPCMIsFloatKey) = false
outputSettings.Value(AVFoundationMBS.AVLinearPCMIsBigEndianKey) = false
outputSettings.Value(AVFoundationMBS.AVFormatIDKey) = OSTypeFromStringMBS(AVFoundationMBS.kAudioFormatLinearPCM)
outputSettings.Value(AVFoundationMBS.AVSampleRateKey) = 44100.0
outputSettings.Value(AVFoundationMBS.AVNumberOfChannelsKey) = 1
outputSettings.Value(AVFoundationMBS.AVChannelLayoutKey) = channelLayout.Memory
assetWriterInput = AVAssetWriterInputMBS.assetWriterInputWithMediaType(AVFoundationMBS.AVMediaTypeAudio, outputSettings)
if assetWriter.canAddInput(assetWriterInput) then
assetWriter.addInput(assetWriterInput)
else
break
MsgBox "can't add asset writer input... die!"
return
end if
assetWriterInput.expectsMediaDataInRealTime = false
call assetWriter.startWriting
assetReader.startReading
dim soundTrack as AVAssetTrackMBS = tracks(0)
dim startTime as CMTimeMBS = CMTimeMBS.Make(0, soundTrack.naturalTimeScale)
assetWriter.startSessionAtSourceTime(startTime)
'assetWriterInput.check
// here we pass ID for job so we can later find it back
assetWriterInput.requestMediaDataWhenReadyOnQueue(assetWriterInput, AssetReaderOutput)
End Sub
Sub add(f as FolderItem)
List.AddRow f.DisplayName
List.RowTag(List.LastIndex) = f
End Sub
Property AVFoundation As MyAVFoundation
Property assetReader As AVAssetReaderMBS
Property assetReaderOutput As AVAssetReaderOutputMBS
Property assetWriter As AVAssetWriterMBS
Property assetWriterInput As AVAssetWriterInputMBS
Property exportFile As FolderItem
Property songAsset As AVURLAssetMBS
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 MyAVFoundation Inherits AVFoundationMBS
EventHandler Sub requestMediaDataWhenReadyOnQueueFinished(assetWriterInput as AVAssetWriterInputMBS, assetReaderOutput as AVAssetReaderOutputMBS, tag as integer)
// we do all the stuff with IDs and operations for this. To find the object here!
MainWindow.Cleanup
MainWindow.NextFile
End EventHandler
End Class
FileTypes1
Filetype audio/mp3
Filetype video/mpeg
Filetype video/quicktime
Filetype audio/aac
Filetype audio/aiff
Filetype special/folder
Filetype audio/x-m4a
Filetype audio/x-m4b
Filetype audio/x-m4p
End FileTypes1
End Project
See also:
The items on this page are in the following plugins: MBS AVFoundation Plugin.