dump_playlist.cxx 3.26 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
#include "config/Global.hxx"
26
#include "decoder/DecoderList.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "input/Init.hxx"
28
#include "event/Thread.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 "thread/Cond.hxx"
35
#include "util/PrintException.hxx"
36

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

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

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 Path config_path = Path::FromFS(argv[1]);
59
	uri = argv[2];
60 61 62 63

	/* initialize MPD */

	config_global_init();
64

65
	ReadConfigFile(config_path);
66

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

70
	input_stream_global_init(GetGlobalConfig(), io_thread.GetEventLoop());
71
	playlist_list_global_init(GetGlobalConfig());
72
	decoder_plugin_init_all(GetGlobalConfig());
73

74
	/* open the playlist */
75

76
	Mutex mutex;
77

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

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

85 86
		/* open the playlist */

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

	/* dump the playlist */

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

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

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

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

	/* deinitialize everything */

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

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

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