diff options
author | mio <stigma@disroot.org> | 2025-03-25 16:36:51 +1000 |
---|---|---|
committer | mio <stigma@disroot.org> | 2025-03-27 12:48:06 +1000 |
commit | e3a8e682329749071271d9a8a4de37ca1745446c (patch) | |
tree | 7b7bcad8b8ec63dd42a6e0a10f3fec3283ad3ff1 | |
parent | aa2b0c1cfc5541545d167a70b1a160fd7022dbbd (diff) | |
download | tdemultimedia-e3a8e682329749071271d9a8a4de37ca1745446c.tar.gz tdemultimedia-e3a8e682329749071271d9a8a4de37ca1745446c.zip |
tdefile-plugin: Add MP4 plugin
Signed-off-by: mio <stigma@disroot.org>
-rw-r--r-- | tdefile-plugins/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tdefile-plugins/mp4/CMakeL10n.txt | 3 | ||||
-rw-r--r-- | tdefile-plugins/mp4/CMakeLists.txt | 42 | ||||
-rw-r--r-- | tdefile-plugins/mp4/tdefile_mp4.cpp | 276 | ||||
-rw-r--r-- | tdefile-plugins/mp4/tdefile_mp4.desktop | 8 | ||||
-rw-r--r-- | tdefile-plugins/mp4/tdefile_mp4.h | 39 |
6 files changed, 369 insertions, 0 deletions
diff --git a/tdefile-plugins/CMakeLists.txt b/tdefile-plugins/CMakeLists.txt index 526e203e..09856785 100644 --- a/tdefile-plugins/CMakeLists.txt +++ b/tdefile-plugins/CMakeLists.txt @@ -14,6 +14,7 @@ add_subdirectory( avi ) tde_conditional_add_subdirectory( WITH_TAGLIB flac ) add_subdirectory( m3u ) tde_conditional_add_subdirectory( WITH_TAGLIB mp3 ) +tde_conditional_add_subdirectory( WITH_TAGLIB mp4 ) tde_conditional_add_subdirectory( HAVE_TAGLIB_MPC_H mpc ) add_subdirectory( mpeg ) tde_conditional_add_subdirectory( WITH_VORBIS ogg ) diff --git a/tdefile-plugins/mp4/CMakeL10n.txt b/tdefile-plugins/mp4/CMakeL10n.txt new file mode 100644 index 00000000..aa8be462 --- /dev/null +++ b/tdefile-plugins/mp4/CMakeL10n.txt @@ -0,0 +1,3 @@ +##### create translation templates ############## + +tde_l10n_create_template( "tdefile_mp4" ) diff --git a/tdefile-plugins/mp4/CMakeLists.txt b/tdefile-plugins/mp4/CMakeLists.txt new file mode 100644 index 00000000..17e388ac --- /dev/null +++ b/tdefile-plugins/mp4/CMakeLists.txt @@ -0,0 +1,42 @@ +################################################# +# +# (C) 2025 mio <stigma@disroot.org> +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +include_directories( + ${CMAKE_BINARY_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ${TAGLIB_INCLUDE_DIRS} + ${TQT_INCLUDE_DIRS} + ${TDE_INCLUDE_DIR} +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + + +##### tdefile_mp4 (module) ###################### + +tde_add_kpart( tdefile_mp4 AUTOMOC + SOURCES + tdefile_mp4.cpp + LINK + DCOP-shared tdecore-shared tdeui-shared tdefx-shared tdeio-shared + tdetexteditor-shared ${TAGLIB_LIBRARIES} + DESTINATION ${PLUGIN_INSTALL_DIR} +) + + +##### other data ################################ + +tde_create_translated_desktop( + SOURCE tdefile_mp4.desktop + DESTINATION ${SERVICES_INSTALL_DIR} + PO_DIR tdefile-desktops +) diff --git a/tdefile-plugins/mp4/tdefile_mp4.cpp b/tdefile-plugins/mp4/tdefile_mp4.cpp new file mode 100644 index 00000000..04291b2d --- /dev/null +++ b/tdefile-plugins/mp4/tdefile_mp4.cpp @@ -0,0 +1,276 @@ +/* This file is part of the TDE project + * Copyright (C) 2025 mio <stigma@disroot.org> + * + * Portions of code based off tdefile_mp3.cpp + * Copyright (C) 2001, 2002 Rolf Magnus <ramagnus@kde.org> + * Copyright (C) 2002 Ryan Cumming <bodnar42@phalynx.dhs.org> + * Copyright (C) 2003 Scott Wheeler <wheeler@kde.org> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation version 2. + * + * 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#include "config.h" +#include "tdefile_mp4.h" + +// TQt +#include <tqfile.h> + +// TDE +#include <kgenericfactory.h> +#include <kstringvalidator.h> + +// TagLib +#include <id3v1genres.h> +#include <id3v2tag.h> +#include <mp4file.h> +#include <tfilestream.h> +#include <tpropertymap.h> +#include <tstring.h> + +#define TStringToTQString(s) TQString::fromUtf8(s.toCString(true)) + +using Mp4Factory = KGenericFactory<TDEMp4Plugin>; + +K_EXPORT_COMPONENT_FACTORY(tdefile_mp4, Mp4Factory("tdefile_mp4")) + +/** + * Do translation between KFileMetaInfo items and TagLib::String in a tidy way. + */ +class Translator +{ +public: + Translator(const KFileMetaInfo &info) : m_info(info) {} + + TagLib::String operator[](const char *key) const + { + return QStringToTString(m_info["id3"][key].value().toString()); + } + + int toInt(const char *key) const + { + return m_info["id3"][key].value().toInt(); + } + +private: + const KFileMetaInfo &m_info; +}; + +class Validator : public KStringListValidator +{ +public: + Validator(const TQStringList& list, bool rejecting, bool fixupEnabled, + TQObject *parent, const char *name) + : KStringListValidator(list, rejecting, fixupEnabled, parent, name) + { + + } + + TQValidator::State validate(TQString&, int&) const override + { + return TQValidator::Acceptable; + } +}; + +TDEMp4Plugin::TDEMp4Plugin(TQObject *parent, const char *name, const TQStringList& args) + : KFilePlugin(parent, name, args) +{ + KFileMimeTypeInfo *info = addMimeTypeInfo(name); + + // ID3 group + KFileMimeTypeInfo::GroupInfo *group = addGroupInfo(info, "id3", i18n("ID3 Tag")); + setAttributes(group, KFileMimeTypeInfo::Addable | KFileMimeTypeInfo::Removable); + + KFileMimeTypeInfo::ItemInfo *item; + + item = addItemInfo(group, "Title", i18n("Title"), TQVariant::String); + setAttributes(item, KFileMimeTypeInfo::Modifiable); + setHint(item, KFileMimeTypeInfo::Name); + + item = addItemInfo(group, "Artist", i18n("Artist"), TQVariant::String); + setAttributes(item, KFileMimeTypeInfo::Modifiable); + setHint(item, KFileMimeTypeInfo::Author); + + item = addItemInfo(group, "Album", i18n("Album"), TQVariant::String); + setAttributes(item, KFileMimeTypeInfo::Modifiable); + + item = addItemInfo(group, "Date", i18n("Year"), TQVariant::String); + setAttributes(item, KFileMimeTypeInfo::Modifiable); + + item = addItemInfo(group, "Comment", i18n("Comment"), TQVariant::String); + setAttributes(item, KFileMimeTypeInfo::Modifiable); + setHint(item, KFileMimeTypeInfo::Description); + + item = addItemInfo(group, "Tracknumber", i18n("Track"), TQVariant::Int); + setAttributes(item, KFileMimeTypeInfo::Modifiable); + + item = addItemInfo(group, "Genre", i18n("Genre"), TQVariant::String); + setAttributes(item, KFileMimeTypeInfo::Modifiable); + + // Technical group. + group = addGroupInfo(info, "Technical", i18n("Technical Details")); + + item = addItemInfo(group, "Length", i18n("Length"), TQVariant::Int); + setAttributes(item, KFileMimeTypeInfo::Cummulative); + setUnit(item, KFileMimeTypeInfo::Seconds); + + item = addItemInfo(group, "Bitrate", i18n("Average Bitrate"), TQVariant::Int); + setAttributes(item, KFileMimeTypeInfo::Averaged); + setHint(item, KFileMimeTypeInfo::Bitrate); + setSuffix(item, i18n(" kbps")); + + item = addItemInfo(group, "Sample Rate", i18n("Sample Rate"), TQVariant::Int); + setSuffix(item, i18n("Hz")); + + item = addItemInfo(group, "Channels", i18n("Channels"), TQVariant::Int); +} + +TQValidator* TDEMp4Plugin::createValidator(const TQString& mimeType, const TQString& group, + const TQString& key, TQObject *parent, const char* name) const +{ + if (key == "Tracknumber" || key == "Date") + { + return new TQIntValidator(0, 9999, parent, name); + } + + if (key == "Genre") + { + TQStringList genres; + for (const auto& genre : TagLib::ID3v1::genreList()) + { + genres.append(TStringToTQString(genre)); + } + + return new Validator(genres, false, true, parent, name); + } + + return nullptr; +} + +bool TDEMp4Plugin::readInfo(KFileMetaInfo& info, unsigned what) +{ + using What = KFileMetaInfo::What; + + bool readID3 = false; + bool readTech = false; + + if (what & (What::Fastest | What::DontCare | What::ContentInfo)) + { + readID3 = true; + } + + if (what & (What::Fastest | What::DontCare | What::TechnicalInfo)) + { + readTech = true; + } + + if (!readID3 && !readTech) + { + return true; + } + + if (info.path().isNull()) + { + // Remote file. + return false; + } + + TagLib::MP4::File file(TQFile::encodeName(info.path()), readTech); + + if (!file.isOpen()) + { + kdWarning(7034) << "Could not open " << file.name() << endl; + return false; + } + + if (readID3) + { + KFileMetaInfoGroup id3group = appendGroup(info, "id3"); + const TagLib::MP4::Tag* tag = file.tag(); + + if (!tag->title().isEmpty()) + { + appendItem(id3group, "Title", TStringToTQString(tag->title()).stripWhiteSpace()); + } + if (!tag->artist().isEmpty()) + { + appendItem(id3group, "Artist", TStringToTQString(tag->artist()).stripWhiteSpace()); + } + if (!tag->album().isEmpty()) + { + appendItem(id3group, "Album", TStringToTQString(tag->album()).stripWhiteSpace()); + } + if (!tag->comment().isEmpty()) + { + appendItem(id3group, "Comment", TStringToTQString(tag->comment()).stripWhiteSpace()); + } + if (!tag->genre().isEmpty()) + { + appendItem(id3group, "Genre", TStringToTQString(tag->genre()).stripWhiteSpace()); + } + + TQString date = (tag->year() > 0) ? TQString::number(tag->year()) : TQString::null; + TQString track = (tag->track() > 0) ? TQString::number(tag->track()) : TQString::null; + + appendItem(id3group, "Date", date); + appendItem(id3group, "Tracknumber", track); + } + + if (readTech) + { + KFileMetaInfoGroup techGroup = appendGroup(info, "Technical"); + const TagLib::AudioProperties* audioProperties = file.audioProperties(); + + appendItem(techGroup, "Length", audioProperties->lengthInSeconds()); + appendItem(techGroup, "Bitrate", audioProperties->bitrate()); + appendItem(techGroup, "Sample Rate", audioProperties->sampleRate()); + appendItem(techGroup, "Channels", audioProperties->channels()); + } + + return true; +} + +bool TDEMp4Plugin::writeInfo(const KFileMetaInfo& info) const +{ + TagLib::ID3v2::FrameFactory::instance()->setDefaultTextEncoding(TagLib::String::UTF8); + TagLib::FileStream stream(TQFile::encodeName(info.path()), false); + + if (!stream.isOpen() || stream.readOnly()) + { + kdDebug(7034) << "couldn't open " << info.path() << " for writing" << endl; + return false; + } + + TagLib::MP4::File file(&stream, false); + + if (!file.isValid()) + { + return false; + } + + Translator t(info); + + file.tag()->setTitle(t["Title"]); + file.tag()->setArtist(t["Artist"]); + file.tag()->setAlbum(t["Album"]); + file.tag()->setYear(t.toInt("Date")); + file.tag()->setComment(t["Comment"]); + file.tag()->setTrack(t.toInt("Tracknumber")); + file.tag()->setGenre(t["Genre"]); + + file.save(); + + return true; +} + +#include "tdefile_mp4.moc" diff --git a/tdefile-plugins/mp4/tdefile_mp4.desktop b/tdefile-plugins/mp4/tdefile_mp4.desktop new file mode 100644 index 00000000..7730b871 --- /dev/null +++ b/tdefile-plugins/mp4/tdefile_mp4.desktop @@ -0,0 +1,8 @@ +[Desktop Entry] +Type=Service +Name=MP4 Info +X-TDE-ServiceTypes=KFilePlugin +X-TDE-Library=tdefile_mp4 +MimeType=audio/mp4 +PreferredGroups=id3,Technical +PreferredItems=Title,Artist,Album,Genre,Date,Tracknumber,Length,Comment,Bitrate,Sample Rate,Channels diff --git a/tdefile-plugins/mp4/tdefile_mp4.h b/tdefile-plugins/mp4/tdefile_mp4.h new file mode 100644 index 00000000..16e9b65a --- /dev/null +++ b/tdefile-plugins/mp4/tdefile_mp4.h @@ -0,0 +1,39 @@ +/* This file is part of the TDE project + * Copyright (C) 2025 mio <stigma@disroot.org> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation version 2. + * + * 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#ifndef __TDEFILE_MP4_H__ +#define __TDEFILE_MP4_H__ + +#include <tdefilemetainfo.h> + +class TQStringList; + +class TDEMp4Plugin : public KFilePlugin +{ + TQ_OBJECT + +public: + TDEMp4Plugin(TQObject *parent, const char *name, const TQStringList& meta); + + TQValidator* createValidator(const TQString& mimeType, const TQString& group, + const TQString& key, TQObject *parent, const char* name) const override; + + bool readInfo(KFileMetaInfo& info, unsigned what) override; + bool writeInfo(const KFileMetaInfo& info) const override; +}; + +#endif /* __TDEFILE_MP4_H__ */ |