dump_playlist.cxx 3.2 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "TagSave.hxx"
21
#include "song/DetachedSong.hxx"
22
#include "playlist/SongEnumerator.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "input/InputStream.hxx"
24
#include "ConfigGlue.hxx"
25
#include "decoder/DecoderList.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "input/Init.hxx"
27
#include "event/Thread.hxx"
28 29
#include "playlist/PlaylistRegistry.hxx"
#include "playlist/PlaylistPlugin.hxx"
30
#include "fs/Path.hxx"
31
#include "fs/NarrowPath.hxx"
32 33
#include "fs/io/BufferedOutputStream.hxx"
#include "fs/io/StdioOutputStream.hxx"
34
#include "thread/Cond.hxx"
35
#include "util/PrintException.hxx"
36

37
#include <unistd.h>
38
#include <stdlib.h>
39

40 41 42 43
static void
tag_save(FILE *file, const Tag &tag)
{
	StdioOutputStream sos(file);
44 45 46
	WithBufferedOutputStream(sos, [&](auto &bos){
		tag_save(bos, tag);
	});
47 48
}

49
int main(int argc, char **argv)
50
try {
51 52
	const char *uri;

53
	if (argc != 3) {
54 55
		fprintf(stderr, "Usage: dump_playlist CONFIG URI\n");
		return EXIT_FAILURE;
56 57
	}

58
	const FromNarrowPath config_path = argv[1];
59
	uri = argv[2];
60 61 62

	/* initialize MPD */

63
	const auto config = AutoLoadConfigFile(config_path);
64

65 66
	EventThread io_thread;
	io_thread.Start();
67

68
	const ScopeInputPluginsInit input_plugins_init(config, io_thread.GetEventLoop());
69
	const ScopePlaylistPluginsInit playlist_plugins_init(config);
70
	const ScopeDecoderPluginsInit decoder_plugins_init(config);
71

72
	/* open the playlist */
73

74
	Mutex mutex;
75

76
	InputStreamPtr is;
77
	auto playlist = playlist_list_open_uri(uri, mutex);
78
	if (playlist == nullptr) {
79
		/* open the stream and wait until it becomes ready */
80

81
		is = InputStream::OpenReady(uri, mutex);
82

83 84
		/* open the playlist */

85
		playlist = playlist_list_open_stream(std::move(is), uri);
86
		if (playlist == nullptr) {
87
			fprintf(stderr, "Failed to open playlist\n");
88 89
			return 2;
		}
90 91 92 93
	}

	/* dump the playlist */

94
	std::unique_ptr<DetachedSong> song;
95
	while ((song = playlist->NextSong()) != nullptr) {
96 97
		printf("%s\n", song->GetURI());

98 99
		const unsigned start_ms = song->GetStartTime().ToMS();
		const unsigned end_ms = song->GetEndTime().ToMS();
100

101
		if (end_ms > 0)
102
			printf("range: %u:%02u..%u:%02u\n",
103 104 105 106 107
			       start_ms / 60000,
			       (start_ms / 1000) % 60,
			       end_ms / 60000,
			       (end_ms / 1000) % 60);
		else if (start_ms > 0)
108
			printf("range: %u:%02u..\n",
109 110
			       start_ms / 60000,
			       (start_ms / 1000) % 60);
111

112
		tag_save(stdout, song->GetTag());
113 114 115 116
	}

	/* deinitialize everything */

117
	playlist.reset();
118
	is.reset();
119

120
	return EXIT_SUCCESS;
121 122
} catch (...) {
	PrintException(std::current_exception());
123
	return EXIT_FAILURE;
124
}