dump_playlist.cxx 3.26 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 25 26
#include "config/File.hxx"
#include "config/Migrate.hxx"
#include "config/Data.hxx"
27
#include "decoder/DecoderList.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "input/Init.hxx"
29
#include "event/Thread.hxx"
30 31
#include "playlist/PlaylistRegistry.hxx"
#include "playlist/PlaylistPlugin.hxx"
32
#include "fs/Path.hxx"
33 34
#include "fs/io/BufferedOutputStream.hxx"
#include "fs/io/StdioOutputStream.hxx"
35
#include "thread/Cond.hxx"
36
#include "util/PrintException.hxx"
37

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

41 42 43 44 45 46 47 48 49
static void
tag_save(FILE *file, const Tag &tag)
{
	StdioOutputStream sos(file);
	BufferedOutputStream bos(sos);
	tag_save(bos, tag);
	bos.Flush();
}

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

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

59
	const Path config_path = Path::FromFS(argv[1]);
60
	uri = argv[2];
61 62 63

	/* initialize MPD */

64 65 66
	ConfigData config;
	ReadConfigFile(config, config_path);
	Migrate(config);
67

68 69
	EventThread io_thread;
	io_thread.Start();
70

71 72 73
	input_stream_global_init(config, io_thread.GetEventLoop());
	playlist_list_global_init(config);
	decoder_plugin_init_all(config);
74

75
	/* open the playlist */
76

77
	Mutex mutex;
78

79
	InputStreamPtr is;
80
	auto playlist = playlist_list_open_uri(uri, mutex);
81 82
	if (playlist == NULL) {
		/* open the stream and wait until it becomes ready */
83

84
		is = InputStream::OpenReady(uri, mutex);
85

86 87
		/* open the playlist */

88
		playlist = playlist_list_open_stream(std::move(is), uri);
89
		if (playlist == NULL) {
90
			fprintf(stderr, "Failed to open playlist\n");
91 92
			return 2;
		}
93 94 95 96
	}

	/* dump the playlist */

97
	std::unique_ptr<DetachedSong> song;
98
	while ((song = playlist->NextSong()) != NULL) {
99 100
		printf("%s\n", song->GetURI());

101 102
		const unsigned start_ms = song->GetStartTime().ToMS();
		const unsigned end_ms = song->GetEndTime().ToMS();
103

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

115
		tag_save(stdout, song->GetTag());
116 117 118 119
	}

	/* deinitialize everything */

120
	playlist.reset();
121
	is.reset();
122

123
	decoder_plugin_deinit_all();
124 125 126
	playlist_list_global_finish();
	input_stream_global_finish();

127
	return EXIT_SUCCESS;
128 129
} catch (...) {
	PrintException(std::current_exception());
130
	return EXIT_FAILURE;
131
}