Warning, /multimedia/kid3/src/qml/script/ExportJson.qml is written in an unsupported language. File is not indexed.
0001 /** 0002 * \file ExportJson.qml 0003 * Export all tags of all files to a JSON file. 0004 * 0005 * \b Project: Kid3 0006 * \author Urs Fleisch 0007 * \date 29 Oct 2021 0008 * 0009 * Copyright (C) 2021 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.0 0025 0026 Kid3Script { 0027 onRun: { 0028 var obj = {data: []} 0029 var selectedFramesV1 = null 0030 var selectedFramesV2 = null 0031 var selectedFramesV3 = null 0032 0033 /** 0034 * Get list of frame names which are selected in frame table. 0035 * @param tagNr Frame.Tag_1, Frame.Tag_2, or Frame.Tag_3 0036 * @return selected frame names, null if all frames are selected. 0037 */ 0038 function getSelectedFrames(tagNr) { 0039 var checked = [] 0040 var frameModel = app.tag(tagNr).frameModel 0041 var numRows = frameModel.rowCount() 0042 for (var row = 0; row < numRows; ++row) { 0043 var name = script.getRoleData(frameModel, row, "name") 0044 if (script.getRoleData(frameModel, row, "checkState") === Qt.Checked) { 0045 checked.push(name) 0046 } 0047 } 0048 return checked.length < numRows ? checked : null 0049 } 0050 0051 /** 0052 * Remove all frames from tags which are not included in @a selectedFrames. 0053 * @param tags object with frame names as keys 0054 * @param selectedFrames array with keys which will not be removed, 0055 * if null, nothing will be removed 0056 */ 0057 function removeUnselectedFrames(tags, selectedFrames) { 0058 if (selectedFrames) { 0059 for (var name in tags) { 0060 if (tags.hasOwnProperty(name)) { 0061 if (!selectedFrames.includes(name)) { 0062 delete tags[name] 0063 } 0064 } 0065 } 0066 } 0067 } 0068 0069 function doWork() { 0070 var tags 0071 var prop 0072 if (app.selectionInfo.tag(Frame.Tag_2).tagFormat) { 0073 tags = app.getAllFrames(tagv2) 0074 removeUnselectedFrames(tags, selectedFramesV2) 0075 } 0076 if (app.selectionInfo.tag(Frame.Tag_1).tagFormat) { 0077 var tagsV1 = app.getAllFrames(tagv1) 0078 removeUnselectedFrames(tagsV1, selectedFramesV1) 0079 if (typeof tags === "undefined") { 0080 tags = {} 0081 } 0082 for (prop in tagsV1) { 0083 tags["v1" + prop] = tagsV1[prop] 0084 } 0085 } 0086 if (app.selectionInfo.tag(Frame.Tag_3).tagFormat) { 0087 var tagsV3 = app.getAllFrames(Frame.TagV3) 0088 removeUnselectedFrames(tagsV3, selectedFramesV3) 0089 if (typeof tags === "undefined") { 0090 tags = {} 0091 } 0092 for (prop in tagsV3) { 0093 tags["v3" + prop] = tagsV3[prop] 0094 } 0095 } 0096 if (tags) { 0097 // Feel free to add additional elements, but you may have to exclude 0098 // them in ImportJson.qml too. 0099 // tags["Duration"] = app.selectionInfo.formatString(Frame.Tag_2, "%{duration}") 0100 // tags["Bitrate"] = app.selectionInfo.formatString(Frame.Tag_2, "%{bitrate}") 0101 // tags["Mode"] = app.selectionInfo.formatString(Frame.Tag_2, "%{mode}") 0102 // tags["Codec"] = app.selectionInfo.formatString(Frame.Tag_2, "%{codec}") 0103 // tags["Directory"] = app.selectionInfo.formatString(Frame.Tag_2, "%{dirname}") 0104 // tags["File"] = app.selectionInfo.formatString(Frame.Tag_2, "%{file}") 0105 obj.data.push(tags) 0106 tags["File Path"] = app.selectionInfo.filePath 0107 } 0108 0109 if (!app.nextFile()) { 0110 var txt = JSON.stringify(obj) 0111 var exportPath = getArguments()[0] 0112 if (!exportPath) { 0113 exportPath = app.selectFileName( 0114 "Export", app.dirName + "/export.json", 0115 "JSON files (*.json);;All files (*)", true) 0116 if (!exportPath) { 0117 Qt.quit() 0118 return 0119 } 0120 } 0121 if (script.writeFile(exportPath, txt)) { 0122 console.log("Exported tags of %1 files to %2". 0123 arg(obj.data.length).arg(exportPath)) 0124 } else { 0125 console.log("Failed to write", exportPath) 0126 } 0127 Qt.quit() 0128 } else { 0129 setTimeout(doWork, 1) 0130 } 0131 } 0132 0133 function startWork() { 0134 selectedFramesV1 = getSelectedFrames(Frame.Tag_1) 0135 selectedFramesV2 = getSelectedFrames(Frame.Tag_2) 0136 selectedFramesV3 = getSelectedFrames(Frame.Tag_3) 0137 0138 app.expandFileListFinished.disconnect(startWork) 0139 console.log("Reading tags") 0140 app.firstFile() 0141 doWork() 0142 } 0143 0144 if (!isStandalone() && app.hasGui()) { 0145 console.log("Expanding file list") 0146 app.expandFileListFinished.connect(startWork) 0147 app.requestExpandFileList() 0148 } else { 0149 startWork() 0150 } 0151 } 0152 }