Commit a936cb23 authored by Urs Fleisch's avatar Urs Fleisch
Browse files

QML: Scripts to extract and embed album art.

parent 8178a450
......@@ -141,6 +141,10 @@ void UserActionsConfig::readFromConfig(ISettings* config)
UserActionsConfig::MenuCommand(QLatin1String("ReplayGain to SoundCheck"), QLatin1String("@qml %{qmlpath}/script/ReplayGain2SoundCheck.qml"), false, true));
m_contextMenuCommands.push_back(
UserActionsConfig::MenuCommand(QLatin1String("Resize Album Art"), QLatin1String("@qml %{qmlpath}/script/ResizeAlbumArt.qml"), false, true));
m_contextMenuCommands.push_back(
UserActionsConfig::MenuCommand(QLatin1String("Extract Album Art"), QLatin1String("@qml %{qmlpath}/script/ExtractAlbumArt.qml"), false, true));
m_contextMenuCommands.push_back(
UserActionsConfig::MenuCommand(QLatin1String("Embed Album Art"), QLatin1String("@qml %{qmlpath}/script/EmbedAlbumArt.qml"), false, true));
m_contextMenuCommands.push_back(
UserActionsConfig::MenuCommand(QLatin1String("Text Encoding ID3v1"), QLatin1String("@qml %{qmlpath}/script/ShowTextEncodingV1.qml"), false, true));
m_contextMenuCommands.push_back(
......
/**
* \file EmbedAlbumArt.qml
* Embed cover art found in image files into audio files in the same folder.
*
* \b Project: Kid3
* \author Urs Fleisch
* \date 08 Mar 2015
*
* Copyright (C) 2015 Urs Fleisch
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Kid3 1.0
Kid3Script {
onRun: {
var lastDir, picName, picData
var baseName = configs.fileConfig().defaultCoverFileName
function doWork() {
if (app.selectionInfo.tagFormatV2 || app.selectionInfo.tagFormatV1) {
var fileName = app.selectionInfo.fileName
var dirName = app.selectionInfo.filePath
dirName = dirName.substring(0, dirName.length - fileName.length)
if (dirName !== lastDir) {
lastDir = dirName
var existingImageFiles = script.listDir(dirName, ["*.png", "*.jpg"])
picName = ""
picData = null
if (existingImageFiles.indexOf(baseName) !== -1) {
picName = baseName
} else if (existingImageFiles.length > 0) {
picName = existingImageFiles[0]
}
if (picName) {
picName = dirName + picName
picData = script.readFile(picName)
if (script.getDataSize(picData) === 0) {
picData = null
}
}
}
if (picData) {
if (script.getDataSize(app.getPictureData()) === 0) {
app.setPictureData(picData)
console.log("%1: Embedding picture from %2".
arg(fileName).arg(picName))
} else {
console.log(fileName + ": Already contains picture")
}
}
}
if (!app.nextFile()) {
if (isStandalone()) {
// Save the changes if the script is started stand-alone, not from Kid3.
app.saveDirectory()
}
Qt.quit()
} else {
setTimeout(doWork, 1)
}
}
app.firstFile()
doWork()
}
}
/**
* \file ExtractAlbumArt.qml
* Extract all embedded cover art pictures avoiding duplicates.
*
* \b Project: Kid3
* \author Urs Fleisch
* \date 08 Mar 2015
*
* Copyright (C) 2015 Urs Fleisch
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Kid3 1.0
Kid3Script {
onRun: {
var lastDir
var md5Map = {}
var baseName = configs.fileConfig().defaultCoverFileName
var extPos = baseName.lastIndexOf(".")
if (extPos !== -1) {
baseName = baseName.substr(0, extPos)
}
function doWork() {
if (app.selectionInfo.tagFormatV2) {
var data = app.getPictureData()
if (script.getDataSize(data) !== 0) {
var fileName = app.selectionInfo.fileName
var dirName = app.selectionInfo.filePath
dirName = dirName.substring(0, dirName.length - fileName.length)
if (dirName !== lastDir) {
lastDir = dirName
var existingImageFiles = script.listDir(dirName, ["*.png", "*.jpg"])
for (var i = 0; i < existingImageFiles.length; ++i) {
var filePath = dirName + existingImageFiles[i]
var fileData = script.readFile(filePath)
if (script.getDataSize(fileData) !== 0) {
md5Map[script.getDataMd5(fileData)] = filePath
}
}
}
var md5 = script.getDataMd5(data)
if (md5 in md5Map) {
console.log("Picture in %1 already exists in %2".
arg(fileName).arg(md5Map[md5]))
} else {
var format = "jpg"
var img = script.dataToImage(data, format)
var imgProps = script.imageProperties(img)
if (!("width" in imgProps)) {
format = "png"
img = script.dataToImage(data, format)
imgProps = script.imageProperties(img)
}
if ("width" in imgProps) {
var picPath = dirName + baseName + "." + format
var picNr = 1
while (script.fileExists(picPath)) {
++picNr
picPath = dirName + baseName + picNr + "." + format
}
if (script.writeFile(picPath, data)) {
md5Map[md5] = picPath
console.log("Picture in %1 stored to %2".
arg(fileName).arg(picPath))
} else {
console.log("Failed to write", picPath)
}
}
}
}
}
if (!app.nextFile()) {
Qt.quit()
} else {
setTimeout(doWork, 1)
}
}
app.firstFile()
doWork()
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment