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

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

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

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

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

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

	/* initialize MPD */

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

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

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

76
	/* open the playlist */
77

78
	Mutex mutex;
79

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

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

87 88
		/* open the playlist */

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

	/* dump the playlist */

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

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

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

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

	/* deinitialize everything */

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

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

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