Added playlist import (and sync)from m3u files, ref #391
This commit is contained in:
@@ -12,6 +12,7 @@ add_library(lmsmetadata SHARED
|
||||
impl/AvFormatTagReader.cpp
|
||||
impl/Lyrics.cpp
|
||||
impl/Parser.cpp
|
||||
impl/PlayList.cpp
|
||||
impl/TagLibTagReader.cpp
|
||||
impl/Utils.cpp
|
||||
)
|
||||
|
||||
@@ -269,6 +269,32 @@ namespace lms::metadata
|
||||
}
|
||||
}
|
||||
|
||||
std::span<const std::filesystem::path> Parser::getSupportedExtensions() const
|
||||
{
|
||||
// TODO: use backend capability to retrieve supported formats
|
||||
static const std::array<std::filesystem::path, 18> fileExtensions{
|
||||
".aac",
|
||||
".alac",
|
||||
".aif",
|
||||
".aiff",
|
||||
".ape",
|
||||
".dsf",
|
||||
".flac",
|
||||
".m4a",
|
||||
".m4b",
|
||||
".mp3",
|
||||
".mpc",
|
||||
".oga",
|
||||
".ogg",
|
||||
".opus",
|
||||
".shn",
|
||||
".wav",
|
||||
".wma",
|
||||
".wv",
|
||||
};
|
||||
return fileExtensions;
|
||||
}
|
||||
|
||||
std::unique_ptr<Track> Parser::parse(const std::filesystem::path& p, bool debug)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -34,6 +34,7 @@ namespace lms::metadata
|
||||
std::unique_ptr<Track> parse(const ITagReader& reader);
|
||||
|
||||
private:
|
||||
std::span<const std::filesystem::path> getSupportedExtensions() const override;
|
||||
void setUserExtraTags(std::span<const std::string> extraTags) override { _userExtraTags.assign(std::cbegin(extraTags), std::cend(extraTags)); }
|
||||
void setArtistTagDelimiters(std::span<const std::string> delimiters) override { _artistTagDelimiters.assign(std::cbegin(delimiters), std::cend(delimiters)); }
|
||||
void setDefaultTagDelimiters(std::span<const std::string> delimiters) override { _defaultTagDelimiters.assign(std::cbegin(delimiters), std::cend(delimiters)); }
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS 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, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "metadata/PlayList.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <string_view>
|
||||
|
||||
#include "core/String.hpp"
|
||||
|
||||
namespace lms::metadata
|
||||
{
|
||||
std::span<const std::filesystem::path> getSupportedPlayListFileExtensions()
|
||||
{
|
||||
static const std::array<std::filesystem::path, 2> fileExtensions{ ".m3u", "m3u8" };
|
||||
return fileExtensions;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Comment
|
||||
{
|
||||
std::string_view directive;
|
||||
std::string_view parameter;
|
||||
};
|
||||
|
||||
std::optional<Comment> parseComment(std::string_view line)
|
||||
{
|
||||
if (line.empty() || line.front() != '#')
|
||||
return std::nullopt;
|
||||
|
||||
Comment comment;
|
||||
const std::string_view::size_type parameterSeparator{ line.find(':') };
|
||||
if (parameterSeparator == std::string_view::npos)
|
||||
{
|
||||
comment.directive = line;
|
||||
}
|
||||
else
|
||||
{
|
||||
comment.directive = line.substr(0, parameterSeparator + 1);
|
||||
comment.parameter = line.substr(parameterSeparator + 1);
|
||||
};
|
||||
|
||||
return comment;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
PlayList parsePlayList(std::istream& is)
|
||||
{
|
||||
PlayList playlist;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(is, line))
|
||||
{
|
||||
const std::string_view trimmedLine{ core::stringUtils::stringTrim(line) };
|
||||
if (trimmedLine.empty())
|
||||
continue;
|
||||
|
||||
// Don't enforce #EXTM3U as first line: be permissive
|
||||
if (const std::optional<Comment> comment{ parseComment(trimmedLine) })
|
||||
{
|
||||
if (comment->directive == "#PLAYLIST:")
|
||||
playlist.name = comment->parameter;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// filter out URI = scheme ":" ["//" authority] path ["?" query] ["#" fragment]
|
||||
// Consider an entry with a ':' is actually an url, as filenames are not supposed to have ':' on windows
|
||||
if (trimmedLine.find(':') != std::string_view::npos)
|
||||
continue;
|
||||
|
||||
const std::filesystem::path path{ std::cbegin(trimmedLine), std::cend(trimmedLine) };
|
||||
playlist.files.emplace_back(path.lexically_normal());
|
||||
}
|
||||
|
||||
return playlist;
|
||||
}
|
||||
} // namespace lms::metadata
|
||||
@@ -34,6 +34,7 @@ namespace lms::metadata
|
||||
|
||||
virtual std::unique_ptr<Track> parse(const std::filesystem::path& p, bool debug = false) = 0;
|
||||
|
||||
virtual std::span<const std::filesystem::path> getSupportedExtensions() const = 0;
|
||||
virtual void setUserExtraTags(std::span<const std::string> extraTags) = 0;
|
||||
virtual void setArtistTagDelimiters(std::span<const std::string> delimiters) = 0;
|
||||
virtual void setDefaultTagDelimiters(std::span<const std::string> delimiters) = 0;
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include <filesystem>
|
||||
#include <iosfwd>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS 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, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <iosfwd>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace lms::metadata
|
||||
{
|
||||
struct PlayList
|
||||
{
|
||||
std::string name;
|
||||
std::vector<std::filesystem::path> files;
|
||||
};
|
||||
|
||||
std::span<const std::filesystem::path> getSupportedPlayListFileExtensions();
|
||||
PlayList parsePlayList(std::istream& is);
|
||||
} // namespace lms::metadata
|
||||
@@ -4,6 +4,7 @@ add_executable(test-metadata
|
||||
Lyrics.cpp
|
||||
Metadata.cpp
|
||||
Parser.cpp
|
||||
PlayList.cpp
|
||||
Utils.cpp
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS 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, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "metadata/PlayList.hpp"
|
||||
|
||||
namespace lms::metadata::tests
|
||||
{
|
||||
TEST(PlayList, basic)
|
||||
{
|
||||
std::istringstream is{ R"(#EXTM3U
|
||||
#PLAYLIST:My super playlist
|
||||
01-Foo.mp3
|
||||
|
||||
|
||||
#EXTINF:263,Alice in Chains - Don't Follow
|
||||
02-FooBar.mp3
|
||||
#EXTALB:Album Title (2009)
|
||||
03-Bar.mp3
|
||||
/this is/a test with a long/path and some spaces/foo.mp3
|
||||
and another one/with relative path/foo.mp3
|
||||
one to be/../one to be/normalized/foo.mp3)" };
|
||||
|
||||
const PlayList playlist{ parsePlayList(is) };
|
||||
|
||||
EXPECT_EQ(playlist.name, "My super playlist");
|
||||
ASSERT_EQ(playlist.files.size(), 6);
|
||||
EXPECT_EQ(playlist.files[0], "01-Foo.mp3");
|
||||
EXPECT_EQ(playlist.files[1], "02-FooBar.mp3");
|
||||
EXPECT_EQ(playlist.files[2], "03-Bar.mp3");
|
||||
EXPECT_EQ(playlist.files[3], "/this is/a test with a long/path and some spaces/foo.mp3");
|
||||
EXPECT_EQ(playlist.files[4], "and another one/with relative path/foo.mp3");
|
||||
EXPECT_EQ(playlist.files[5], "one to be/normalized/foo.mp3");
|
||||
}
|
||||
} // namespace lms::metadata::tests
|
||||
Reference in New Issue
Block a user