"Extract Cover Art" column and command

(See Also: This is an alternative "Extract Cover Art" command and column implemented in VBScript using Opus native scripting objects, not requiring any additional software. For a different, but similar script, see Custom Column: MP3 CoverArt Yes/No + 2 extract commands.)

Requires Opus 11.6 or above.

The script adds a single command, ExtractCoverArt, which by default will extract any cover art images of selected audio files and save them to the same folder. You can also use the TYPE argument to restrict the extraction to a single type, for files which have multiple images within them. The extracted images will be named after the name of the audio file with the type of image appended.

The column handler adds a "Cover Art" column which displays the number of cover art images inside each audio file.

This script provides an example of several Opus scripting techniques, including:

  • Adding a custom command and column
  • Querying file metadata
  • Opening a file on disk and writing data to it
  • Displaying a Progress indicator

Download:

Extract Cover Art.osp (1.3 KB)

To install the script, copy the attached .OSP file to your Opus Script Add Ins folder (/dopusdata/Script AddIns) or drag-and-drop it to the Toolbars / Scripts page in Preferences.

Script Code:

The script code is presented below for people interested in looking at it without having to download the file.

option explicit

' Extract Cover Art
' (c) 2014 Jonathan Potter
' 
' This is a script for Directory Opus.
' See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.
' 
' 
' 
' Called by Directory Opus to initialize the script
Function OnInit(initData)
	initData.name = "Extract Cover Art"
	initData.desc = "Extract cover art from audio files"
	initData.copyright = "(c) 2014 Jonathan Potter"
	initData.version = "1.0"
	initData.default_enable = false
	initData.min_version = "11.5.4"

	Dim cmd

	Set cmd = initData.AddCommand
	cmd.name = "ExtractCoverArt"
	cmd.method = "OnExtractCoverArt"
	cmd.desc = "Extract cover art from audio files"
	cmd.label = "ExtractCoverArt"
	cmd.template = "TYPE/K[artist,back,band,bandlogo,composer,conductor,front,icon,illustration,leadartist,leaflet,location,lyricist,media,other,performance,publisherlogo,recording,vidcap]"

	Dim col
	
	Set col = initData.AddColumn
    col.name = "HasCoverArt"
    col.method = "OnHasCoverArt"
    col.label = "Cover Art Images"
	col.header = "Cover Art"
    col.autogroup = False
	col.autorefresh = True
    col.justify = "center"
	col.type = "num"
	
End Function


' Implement the ExtractCoverArt command
Function OnExtractCoverArt(scriptCmdData)

	Dim file, fsUtil, metaData, art, artType, pathArt, fileOut, progBar
	
	If scriptCmdData.func.args.got_arg.type Then
		artType = scriptCmdData.func.args.type
	End If
	
	Set fsUtil = DOpus.FSUtil
	Set progBar = scriptCmdData.func.command.progress
	
	progBar.abort = True
	progBar.delay = True
	progBar.Init scriptCmdData.func.sourcetab
	progBar.AddFiles scriptCmdData.func.command.files.count
	progBar.SetStatus "Extracting Cover Art..."
	progBar.SetTitle "Extract Cover Art"
	progBar.Show 
	
	For Each file in scriptCmdData.func.command.files
		If Not (file.is_dir) Then
		
			if progBar.GetAbortState = "a" Then Exit For
			
			progBar.SetName file.name
			progBar.StepFiles 1
			
			Set metaData = file.metadata
			If metaData = "audio" and metaData.audio.coverart > 0 Then
			
				For Each art in metaData.audio.coverart
				
					if progBar.GetAbortState = "a" Then Exit For
					
					If Len(artType) = 0 Or artType = art Then
					
						Set pathArt = fsUtil.NewPath(file.path)
						pathArt.Add file.name_stem & " (" & art.type & ")"
						pathArt.ext = art.ext
						
						Set fileOut = fsUtil.OpenFile(pathArt, "w")
						If fileOut.error = 0 Then
							fileOut.Write art.data
						End If
						fileOut.Close
						
					End If
					
				Next
				
			End If
			
		End If
	Next

End Function


' Implement the HasCoverArt column
Function OnHasCoverArt(scriptColData)

	If scriptColData.item.metadata = "audio" Then
		scriptColData.value = scriptColData.item.metadata.audio.coverart
		If scriptColData.value = 0 Then
			scriptColData.group = "Songs Without Cover Art"
		Else
			scriptColData.group = "Songs With Cover Art"
		End If
	End If

End Function

Excellent, many thanks for this :thumbsup:

This seems very nice, although I haven't really tested it yet.

It is best when the needed functions/objects are already there without having to resort to other
languages and whatnot to achieve what you want. Obviously you can't add just about everything, but
especially generic file or media-centric items/objects and similar goes a long way to alleviate what JS/VBS lacks to begin with.

Any way to edit this to rename the file to "Folder.jpg"?

Change the line which currently says pathArt.Add file.name_stem & " (" & art.type & ")" to pathArt.Add "Folder".

It won't work properly if the file has more than one coverart image inside it however.