summaryrefslogtreecommitdiff
path: root/src/lib/cover_sheet.cc
blob: c21f28a4dc3bebef8855eb7f479f8a8c6504f6d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
    Copyright (C) 2025 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic 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.

    DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "config.h"
#include "cover_sheet.h"
#include "dcp_content_type.h"
#include "film.h"
#include <dcp/file.h>
#include <dcp/filesystem.h>
#include <dcp/locale_convert.h>
#include <boost/algorithm/string.hpp>

#include "i18n.h"


using std::shared_ptr;
using std::string;
using std::vector;


void
dcpomatic::write_cover_sheet(shared_ptr<const Film> film, boost::filesystem::path dcp_directory, boost::filesystem::path output)
{
	dcp::File file(output, "w");
	if (!file) {
		throw OpenFileError(output, file.open_error(), OpenFileError::WRITE);
	}

	auto text = Config::instance()->cover_sheet();
	boost::algorithm::replace_all(text, "$CPL_NAME", film->name());
	auto cpls = film->cpls();
	if (!cpls.empty()) {
		boost::algorithm::replace_all(text, "$CPL_FILENAME", cpls[0].cpl_file.filename().string());
	}
	boost::algorithm::replace_all(text, "$TYPE", film->dcp_content_type()->pretty_name());
	boost::algorithm::replace_all(text, "$CONTAINER", film->container().container_nickname());

	if (auto audio_language = film->audio_language()) {
		boost::algorithm::replace_all(text, "$AUDIO_LANGUAGE", audio_language->description());
	} else {
		boost::algorithm::replace_all(text, "$AUDIO_LANGUAGE", _("None"));
	}

	auto const subtitle_languages = film->open_text_languages();
	if (subtitle_languages.first) {
		boost::algorithm::replace_all(text, "$SUBTITLE_LANGUAGE", subtitle_languages.first->description());
	} else {
		boost::algorithm::replace_all(text, "$SUBTITLE_LANGUAGE", _("None"));
	}

	boost::uintmax_t size = 0;
	for (
		auto i = dcp::filesystem::recursive_directory_iterator(dcp_directory);
		i != dcp::filesystem::recursive_directory_iterator();
		++i) {
		if (dcp::filesystem::is_regular_file(i->path())) {
			size += dcp::filesystem::file_size(i->path());
		}
	}

	if (size > (1000000000L)) {
		boost::algorithm::replace_all(text, "$SIZE", fmt::format("{}GB", dcp::locale_convert<string>(size / 1000000000.0, 1, true)));
	} else {
		boost::algorithm::replace_all(text, "$SIZE", fmt::format("{}MB", dcp::locale_convert<string>(size / 1000000.0, 1, true)));
	}

	auto ch = audio_channel_types(film->mapped_audio_channels(), film->audio_channels());
	auto description = fmt::format("{}.{}", ch.first, ch.second);

	if (description == "0.0") {
		description = _("None");
	} else if (description == "1.0") {
		description = _("Mono");
	} else if (description == "2.0") {
		description = _("Stereo");
	}
	boost::algorithm::replace_all(text, "$AUDIO", description);

	auto const hmsf = film->length().splitX(film->video_frame_rate());
	string length;
	if (hmsf.h == 0 && hmsf.m == 0) {
		length = fmt::format("{}s", hmsf.s);
	} else if (hmsf.h == 0 && hmsf.m > 0) {
		length = fmt::format("{}m{}s", hmsf.m, hmsf.s);
	} else if (hmsf.h > 0 && hmsf.m > 0) {
		length = fmt::format("{}h{}m{}s", hmsf.h, hmsf.m, hmsf.s);
	}

	boost::algorithm::replace_all(text, "$LENGTH", length);

	auto const markers = film->markers();

	auto marker = [&markers, &text, film](dcp::Marker marker) {
		auto iter = markers.find(marker);
		auto const tag = "$" + marker_to_string(marker);
		auto const tag_line = tag + "_LINE";

		if (iter != markers.end()) {
			auto const timecode = time_to_hmsf(iter->second, film->video_frame_rate());
			boost::algorithm::replace_all(text, tag_line, timecode);
			boost::algorithm::replace_all(text, tag, timecode);
		} else {
			vector<string> before_lines;
			vector<string> after_lines;
			boost::algorithm::split(before_lines, text, boost::is_any_of("\n"));
			if (!before_lines.empty()) {
				before_lines.pop_back();
			}
			for (auto& line: before_lines) {
				if (line.find(tag_line) == std::string::npos) {
					after_lines.push_back(line);
				}
			}
			text.clear();
			for (auto const& line: after_lines) {
				text += line + "\n";
			}

			boost::algorithm::replace_all(text, tag, _("Unknown"));
		}
	};

	marker(dcp::Marker::FFOC);
	marker(dcp::Marker::LFOC);
	marker(dcp::Marker::FFTC);
	marker(dcp::Marker::LFTC);
	marker(dcp::Marker::FFOI);
	marker(dcp::Marker::LFOI);
	marker(dcp::Marker::FFEC);
	marker(dcp::Marker::LFEC);
	marker(dcp::Marker::FFMC);
	marker(dcp::Marker::LFMC);
	marker(dcp::Marker::FFOB);
	marker(dcp::Marker::LFOB);

	file.checked_write(text.c_str(), text.length());
}