Warning, /multimedia/kid3/src/qml/script/ExtractAlbumArt.qml is written in an unsupported language. File is not indexed.

0001 /**
0002  * \file ExtractAlbumArt.qml
0003  * Extract all embedded cover art pictures avoiding duplicates.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 08 Mar 2015
0008  *
0009  * Copyright (C) 2015-2017  Urs Fleisch
0010  *
0011  * This program is free software; you can redistribute it and/or modify
0012  * it under the terms of the GNU Lesser General Public License as published by
0013  * the Free Software Foundation; version 3.
0014  *
0015  * This program is distributed in the hope that it will be useful,
0016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  * GNU Lesser General Public License for more details.
0019  *
0020  * You should have received a copy of the GNU Lesser General Public License
0021  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  */
0023 
0024 import Kid3 1.1
0025 
0026 Kid3Script {
0027   onRun: {
0028     var lastDir
0029     var md5Map = {}
0030     var baseName = configs.fileConfig().defaultCoverFileName
0031     var extPos = baseName.lastIndexOf(".")
0032     if (extPos !== -1) {
0033       baseName = baseName.substr(0, extPos)
0034     }
0035 
0036     function doWork() {
0037       if (app.selectionInfo.tag(Frame.Tag_2).tagFormat) {
0038         var data = app.getPictureData()
0039         if (script.getDataSize(data) !== 0) {
0040           var fileName = app.selectionInfo.fileName
0041           var dirName = app.selectionInfo.filePath
0042           dirName = dirName.substring(0, dirName.length - fileName.length)
0043           if (dirName !== lastDir) {
0044             lastDir = dirName
0045             var existingImageFiles = script.listDir(dirName, ["*.png", "*.jpg", "*.webp"])
0046             for (var i = 0; i < existingImageFiles.length; ++i) {
0047               var filePath = dirName + existingImageFiles[i]
0048               var fileData = script.readFile(filePath)
0049               if (script.getDataSize(fileData) !== 0) {
0050                 md5Map[script.getDataMd5(fileData)] = filePath
0051               }
0052             }
0053           }
0054           var md5 = script.getDataMd5(data)
0055           if (md5 in md5Map) {
0056             console.log("Picture in %1 already exists in %2".
0057                         arg(fileName).arg(md5Map[md5]))
0058           } else {
0059             var formats = ["jpg", "png", "webp"]
0060             for (var fmt in formats) {
0061               var format = formats[fmt]
0062               var img = script.dataToImage(data, format)
0063               var imgProps = script.imageProperties(img)
0064               if ("width" in imgProps) {
0065                 var fileBaseName = baseName
0066                 if (fileBaseName.indexOf("%") !== -1) {
0067                   fileBaseName = app.importFromTagsToSelection(
0068                         Frame.Tag_2, baseName, "%{__return}(.+)")
0069                 }
0070                 var picPath = dirName + fileBaseName + "." + format
0071                 var picNr = 1
0072                 while (script.fileExists(picPath)) {
0073                   ++picNr
0074                   picPath = dirName + fileBaseName + picNr + "." + format
0075                 }
0076                 if (script.writeFile(picPath, data)) {
0077                   md5Map[md5] = picPath
0078                   console.log("Picture in %1 stored to %2".
0079                               arg(fileName).arg(picPath))
0080                 } else {
0081                   console.log("Failed to write", picPath)
0082                 }
0083                 break
0084               }
0085             }
0086           }
0087         }
0088       }
0089       if (!nextFile()) {
0090         Qt.quit()
0091       } else {
0092         setTimeout(doWork, 1)
0093       }
0094     }
0095 
0096     firstFile()
0097     doWork()
0098   }
0099 }