dump_playlist.cxx 3.41 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 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 64

	/* initialize MPD */

	config_global_init();
65 66

	Error error;
67
	ReadConfigFile(config_path);
68

69
	const ScopeIOThread io_thread;
70

71
	if (!input_stream_global_init(error)) {
72
		LogError(error);
73
		return EXIT_FAILURE;
74 75
	}

76
	playlist_list_global_init();
77
	decoder_plugin_init_all();
78

79
	/* open the playlist */
80

81 82
	Mutex mutex;
	Cond cond;
83

84
	InputStreamPtr is;
85
	auto playlist = playlist_list_open_uri(uri, mutex, cond);
86 87
	if (playlist == NULL) {
		/* open the stream and wait until it becomes ready */
88

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

99 100
		/* open the playlist */

101
		playlist = playlist_list_open_stream(std::move(is), uri);
102
		if (playlist == NULL) {
103
			fprintf(stderr, "Failed to open playlist\n");
104 105
			return 2;
		}
106 107 108 109
	}

	/* dump the playlist */

110
	std::unique_ptr<DetachedSong> song;
111
	while ((song = playlist->NextSong()) != NULL) {
112 113
		printf("%s\n", song->GetURI());

114 115
		const unsigned start_ms = song->GetStartTime().ToMS();
		const unsigned end_ms = song->GetEndTime().ToMS();
116

117
		if (end_ms > 0)
118
			printf("range: %u:%02u..%u:%02u\n",
119 120 121 122 123
			       start_ms / 60000,
			       (start_ms / 1000) % 60,
			       end_ms / 60000,
			       (end_ms / 1000) % 60);
		else if (start_ms > 0)
124
			printf("range: %u:%02u..\n",
125 126
			       start_ms / 60000,
			       (start_ms / 1000) % 60);
127

128
		tag_save(stdout, song->GetTag());
129 130 131 132
	}

	/* deinitialize everything */

133
	delete playlist;
134
	is.reset();
135

136
	decoder_plugin_deinit_all();
137 138 139 140
	playlist_list_global_finish();
	input_stream_global_finish();
	config_global_finish();

141 142 143 144 145
	return EXIT_SUCCESS;
 } catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
 }