dump_playlist.cxx 3.49 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "DetachedSong.hxx"
23
#include "playlist/SongEnumerator.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "input/InputStream.hxx"
25
#include "config/ConfigGlobal.hxx"
26
#include "decoder/DecoderList.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "input/Init.hxx"
28
#include "ScopeIOThread.hxx"
29 30
#include "playlist/PlaylistRegistry.hxx"
#include "playlist/PlaylistPlugin.hxx"
31
#include "fs/Path.hxx"
32 33
#include "fs/io/BufferedOutputStream.hxx"
#include "fs/io/StdioOutputStream.hxx"
34
#include "util/Error.hxx"
35
#include "thread/Cond.hxx"
36
#include "Log.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
	const char *uri;
53
	InputStream *is = NULL;
54

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 65

	/* initialize MPD */

	config_global_init();
66 67 68

	Error error;
	if (!ReadConfigFile(config_path, error)) {
69 70
		LogError(error);
		return EXIT_FAILURE;
71 72
	}

73
	const ScopeIOThread io_thread;
74

75
	if (!input_stream_global_init(error)) {
76
		LogError(error);
77
		return EXIT_FAILURE;
78 79
	}

80
	playlist_list_global_init();
81
	decoder_plugin_init_all();
82

83
	/* open the playlist */
84

85 86
	Mutex mutex;
	Cond cond;
87

88
	auto playlist = playlist_list_open_uri(uri, mutex, cond);
89 90
	if (playlist == NULL) {
		/* open the stream and wait until it becomes ready */
91

92
		is = InputStream::OpenReady(uri, mutex, cond, error);
93
		if (is == NULL) {
94
			if (error.IsDefined())
95
				LogError(error);
96
			else
97
				fprintf(stderr,
98
					"InputStream::Open() failed\n");
99
			return 2;
100
		}
101

102 103
		/* open the playlist */

104
		playlist = playlist_list_open_stream(*is, uri);
105
		if (playlist == NULL) {
106
			delete is;
107
			fprintf(stderr, "Failed to open playlist\n");
108 109
			return 2;
		}
110 111 112 113
	}

	/* dump the playlist */

114
	DetachedSong *song;
115
	while ((song = playlist->NextSong()) != NULL) {
116 117
		printf("%s\n", song->GetURI());

118 119
		const unsigned start_ms = song->GetStartTime().ToMS();
		const unsigned end_ms = song->GetEndTime().ToMS();
120

121
		if (end_ms > 0)
122
			printf("range: %u:%02u..%u:%02u\n",
123 124 125 126 127
			       start_ms / 60000,
			       (start_ms / 1000) % 60,
			       end_ms / 60000,
			       (end_ms / 1000) % 60);
		else if (start_ms > 0)
128
			printf("range: %u:%02u..\n",
129 130
			       start_ms / 60000,
			       (start_ms / 1000) % 60);
131

132
		tag_save(stdout, song->GetTag());
133

134
		delete song;
135 136 137 138
	}

	/* deinitialize everything */

139
	delete playlist;
140
	delete is;
141

142
	decoder_plugin_deinit_all();
143 144 145 146
	playlist_list_global_finish();
	input_stream_global_finish();
	config_global_finish();

147 148 149 150 151
	return EXIT_SUCCESS;
 } catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
 }