2 Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 /** @file test/srt_subtitle_test.cc
23 * @brief Test writing DCPs with subtitles from .srt.
29 #include "lib/string_text_file_content.h"
30 #include "lib/dcp_content_type.h"
32 #include "lib/ratio.h"
33 #include "lib/text_content.h"
35 #include <boost/test/unit_test.hpp>
36 #include <boost/algorithm/string.hpp>
42 using std::shared_ptr;
43 using std::make_shared;
44 using namespace dcpomatic;
47 /** Make a very short DCP with a single subtitle from .srt with no specified fonts */
48 BOOST_AUTO_TEST_CASE (srt_subtitle_test)
50 auto film = new_test_film ("srt_subtitle_test");
51 film->set_container (Ratio::from_id ("185"));
52 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
53 film->set_name ("frobozz");
54 film->set_audio_channels (6);
55 film->set_interop (false);
56 auto content = make_shared<StringTextFileContent>("test/data/subrip2.srt");
57 film->examine_and_add_content (content);
58 BOOST_REQUIRE (!wait_for_jobs());
60 content->only_text()->set_use (true);
61 content->only_text()->set_burn (false);
65 dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
66 dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
67 dcp::VerificationNote::Code::MISSING_CPL_METADATA
71 /* Should be blank video with a subtitle MXF */
72 check_dcp ("test/data/srt_subtitle_test", film->dir (film->dcp_name ()));
76 /** Same again but with a `font' specified */
77 BOOST_AUTO_TEST_CASE (srt_subtitle_test2)
79 auto film = new_test_film ("srt_subtitle_test2");
80 film->set_container (Ratio::from_id ("185"));
81 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
82 film->set_name ("frobozz");
83 film->set_audio_channels (6);
84 film->set_interop (false);
85 auto content = make_shared<StringTextFileContent> ("test/data/subrip2.srt");
86 film->examine_and_add_content (content);
87 BOOST_REQUIRE (!wait_for_jobs());
89 content->only_text()->set_use (true);
90 content->only_text()->set_burn (false);
91 /* Use test/data/subrip2.srt as if it were a font file */
92 content->only_text()->fonts().front()->set_file("test/data/subrip2.srt");
97 dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
98 dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
99 dcp::VerificationNote::Code::MISSING_CPL_METADATA
102 /* Should be blank video with a subtitle MXF */
103 check_dcp ("test/data/srt_subtitle_test2", film->dir (film->dcp_name ()));
108 check_subtitle_file (shared_ptr<Film> film, boost::filesystem::path ref)
110 /* Find the subtitle file and check it */
111 check_xml (subtitle_file(film), ref, {"SubtitleID"});
115 /** Make another DCP with a longer .srt file */
116 BOOST_AUTO_TEST_CASE (srt_subtitle_test3)
120 auto content = make_shared<StringTextFileContent>(TestPaths::private_data() / "Ankoemmling_short.srt");
121 auto film = new_test_film2 ("srt_subtitle_test3", { content }, &cl);
123 film->set_name ("frobozz");
124 film->set_interop (true);
125 film->set_audio_channels (6);
127 content->only_text()->set_use (true);
128 content->only_text()->set_burn (false);
130 make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_STANDARD});
132 check_subtitle_file (film, TestPaths::private_data() / "Ankoemmling_short.xml");
138 /** Build a small DCP with no picture and a single subtitle overlaid onto it */
139 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
141 auto film = new_test_film ("srt_subtitle_test4");
142 film->set_container (Ratio::from_id ("185"));
143 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
144 film->set_name ("frobozz");
145 film->set_interop (false);
146 auto content = make_shared<StringTextFileContent>("test/data/subrip2.srt");
147 content->only_text()->set_use (true);
148 content->only_text()->set_burn (false);
149 film->examine_and_add_content (content);
150 BOOST_REQUIRE (!wait_for_jobs());
151 make_and_verify_dcp (
154 dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
155 dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
156 dcp::VerificationNote::Code::MISSING_CPL_METADATA
159 /* Should be blank video with MXF subtitles */
160 check_dcp ("test/data/xml_subtitle_test", film->dir (film->dcp_name ()));
164 /** Check the subtitle XML when there are two subtitle files in the project */
165 BOOST_AUTO_TEST_CASE (srt_subtitle_test5)
167 auto film = new_test_film ("srt_subtitle_test5");
168 film->set_container (Ratio::from_id ("185"));
169 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
170 film->set_name ("frobozz");
171 film->set_interop (true);
172 film->set_sequence (false);
173 for (auto i = 0; i < 2; ++i) {
174 auto content = make_shared<StringTextFileContent>("test/data/subrip2.srt");
175 content->only_text()->set_use (true);
176 content->only_text()->set_burn (false);
177 film->examine_and_add_content (content);
178 BOOST_REQUIRE (!wait_for_jobs());
179 content->set_position (film, DCPTime());
181 make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_STANDARD});
183 check_dcp ("test/data/xml_subtitle_test2", film->dir (film->dcp_name ()));
187 BOOST_AUTO_TEST_CASE (srt_subtitle_test6)
189 auto content = make_shared<StringTextFileContent>("test/data/frames.srt");
190 auto film = new_test_film2 ("srt_subtitle_test6", {content});
191 film->set_interop (false);
192 content->only_text()->set_use (true);
193 content->only_text()->set_burn (false);
194 make_and_verify_dcp (
197 dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
198 dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
199 dcp::VerificationNote::Code::MISSING_CPL_METADATA,
200 dcp::VerificationNote::Code::INVALID_SUBTITLE_DURATION,
201 dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING,
204 check_dcp ("test/data/srt_subtitle_test6", film->dir(film->dcp_name()));
209 /* XXX: this is disabled; there is some difference in font rendering
210 between the test machine and others.
213 /** Test rendering of a SubRip subtitle */
214 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
216 shared_ptr<Film> film = new_test_film ("subrip_render_test");
217 shared_ptr<StringTextFile> content (new StringTextFile("test/data/subrip.srt"));
218 content->examine (shared_ptr<Job> (), true);
219 BOOST_CHECK_EQUAL (content->full_length(), DCPTime::from_seconds ((3 * 60) + 56.471));
221 shared_ptr<SubRipDecoder> decoder (new SubRipDecoder (content));
222 list<ContentStringText> cts = decoder->get_plain_texts (
224 ContentTime::from_seconds (109), ContentTime::from_seconds (110)
227 BOOST_CHECK_EQUAL (cts.size(), 1);
229 PositionImage image = render_text (cts.front().subs, dcp::Size (1998, 1080));
230 write_image (image.image, "build/test/subrip_render_test.png");
231 check_file ("build/test/subrip_render_test.png", "test/data/subrip_render_test.png");