Programmatically add converted video to iTunes using SDK
I've written some simple code which interfaces with HandbrakeCLI to convert a video into a format that iTunes can handle. Once that has finished, I want it to be automatically added to iTunes.
From what I can tell from the iTunes SDK documentation, the following VBScript code should work:
Option Explicit
Dim oiTunes
Set oiTunes = CreateObject("iTunes.Application")
oiTunes.ConvertFile2("D:\Development\VBScript\converted-video.avi")
Set oiTunes = Nothing
Msgbox "Uploaded!"
However, upon running, the "Uploaded!" message appears but iTunes has not imported (or even started to import) any video file.
Can anyone suggest how I can get this working? In addition, if someone can show me how I can also determine whether or not it has f开发者_如何学运维inished the import (as I'd like to rename the meta-data once the import has finished) then I'd really appreciate it.
It seems that oiTunes.ConvertFile2
is a method that returns an 'convert' object. Probably it is converting the file asynchronic, so you can execute other VBScript code in the mean time. However, your code directly kills the oiTunes object, also killing the convert process.
There seems to be two properties you can use to read the status of the convertion: .ProgressValue
and .MaxProgressValue
. So you can create a loop like:
Dim oConvert : Set oConvert = oiTunes.ConvertFile2(...)
Do : Loop while oConvert.ProgressValue < oConvert.MaxProgressValue
Set oConvert = Nothing
Set oiTunes = Nothing
MsgBox "Uploaded!"
I do not have iTunes right now, so I couldn't verify if this will solve you problems, but I hope this is giving you a push in the right direction.
Following the useful pointer by AutomatedChaos, I offer the following working code sample:
Option Explicit
Dim oiTunes, oTracks, oAdd
Set oiTunes = CreateObject("iTunes.Application")
Set oTracks = oiTunes.LibraryPlaylist
Set oAdd = oTracks.AddFile("D:\Development\VBScript\converted-video.mp4")
Do : Loop While oAdd.InProgress = True
Set oAdd = Nothing
Set oTracks = Nothing
Set oiTunes = Nothing
Msgbox "Uploaded!"
Make sure that you can import the video file through the iTunes UI first, because if you can't then this will fail.
There is little point trying to put anything into the Do .. Loop
as a 44 minute video is added on my (reasonably low specification) computer in about 2 seconds.
精彩评论