b6df6e393751f12d695e02cc00ca626162a0fc5d
[dcpomatic.git] / test / srt_subtitle_test.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file  test/srt_subtitle_test.cc
22  *  @brief Test writing DCPs with subtitles from .srt.
23  *  @ingroup specific
24  */
25
26 #include "lib/film.h"
27 #include "lib/text_subtitle_content.h"
28 #include "lib/dcp_content_type.h"
29 #include "lib/font.h"
30 #include "lib/ratio.h"
31 #include "lib/subtitle_content.h"
32 #include "test.h"
33 #include <boost/test/unit_test.hpp>
34 #include <boost/algorithm/string.hpp>
35 #include <list>
36
37 using std::string;
38 using std::list;
39 using boost::shared_ptr;
40
41 /** Make a very short DCP with a single subtitle from .srt with no specified fonts */
42 BOOST_AUTO_TEST_CASE (srt_subtitle_test)
43 {
44         shared_ptr<Film> film = new_test_film ("srt_subtitle_test");
45         film->set_container (Ratio::from_id ("185"));
46         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
47         film->set_name ("frobozz");
48         film->set_audio_channels (6);
49         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
50         film->examine_and_add_content (content);
51         wait_for_jobs ();
52
53         content->subtitle->set_use (true);
54         content->subtitle->set_burn (false);
55         film->make_dcp ();
56         wait_for_jobs ();
57
58         /* Should be blank video with a subtitle MXF */
59         check_dcp ("test/data/srt_subtitle_test", film->dir (film->dcp_name ()));
60 }
61
62 /** Same again but with a `font' specified */
63 BOOST_AUTO_TEST_CASE (srt_subtitle_test2)
64 {
65         shared_ptr<Film> film = new_test_film ("srt_subtitle_test2");
66         film->set_container (Ratio::from_id ("185"));
67         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
68         film->set_name ("frobozz");
69         film->set_audio_channels (6);
70         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
71         film->examine_and_add_content (content);
72         wait_for_jobs ();
73
74         content->subtitle->set_use (true);
75         content->subtitle->set_burn (false);
76         /* Use test/data/subrip2.srt as if it were a font file  */
77         content->subtitle->fonts().front()->set_file (FontFiles::NORMAL, "test/data/subrip2.srt");
78
79         film->make_dcp ();
80         wait_for_jobs ();
81
82         /* Should be blank video with a subtitle MXF */
83         check_dcp ("test/data/srt_subtitle_test2", film->dir (film->dcp_name ()));
84 }
85
86 static void
87 check_subtitle_file (shared_ptr<Film> film, boost::filesystem::path ref)
88 {
89         /* Find the subtitle file and check it */
90         for (
91                 boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (film->directory().get() / film->dcp_name (false));
92                 i != boost::filesystem::directory_iterator ();
93                 ++i) {
94
95                 if (boost::filesystem::is_directory (i->path ())) {
96                         for (
97                                 boost::filesystem::directory_iterator j = boost::filesystem::directory_iterator (i->path ());
98                                 j != boost::filesystem::directory_iterator ();
99                                 ++j) {
100
101                                 if (boost::algorithm::starts_with (j->path().leaf().string(), "sub_")) {
102                                         list<string> ignore;
103                                         ignore.push_back ("SubtitleID");
104                                         check_xml (*j, ref, ignore);
105                                 }
106                         }
107                 }
108         }
109 }
110
111 /** Make another DCP with a longer .srt file */
112 BOOST_AUTO_TEST_CASE (srt_subtitle_test3)
113 {
114         shared_ptr<Film> film = new_test_film ("srt_subtitle_test3");
115
116         film->set_container (Ratio::from_id ("185"));
117         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
118         film->set_name ("frobozz");
119         film->set_interop (true);
120         film->set_audio_channels (6);
121         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, private_data / "Ankoemmling_short.srt"));
122         film->examine_and_add_content (content);
123         wait_for_jobs ();
124
125         content->subtitle->set_use (true);
126         content->subtitle->set_burn (false);
127
128         film->make_dcp ();
129         wait_for_jobs ();
130
131         check_subtitle_file (film, private_data / "Ankoemmling_short.xml");
132 }
133
134 /** Build a small DCP with no picture and a single subtitle overlaid onto it */
135 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
136 {
137         shared_ptr<Film> film = new_test_film ("srt_subtitle_test4");
138         film->set_container (Ratio::from_id ("185"));
139         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
140         film->set_name ("frobozz");
141         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
142         content->subtitle->set_use (true);
143         content->subtitle->set_burn (false);
144         film->examine_and_add_content (content);
145         wait_for_jobs ();
146         film->make_dcp ();
147         wait_for_jobs ();
148
149         /* Should be blank video with MXF subtitles */
150         check_dcp ("test/data/xml_subtitle_test", film->dir (film->dcp_name ()));
151 }
152
153 /** Check the subtitle XML when there are two subtitle files in the project */
154 BOOST_AUTO_TEST_CASE (srt_subtitle_test5)
155 {
156         shared_ptr<Film> film = new_test_film ("srt_subtitle_test5");
157         film->set_container (Ratio::from_id ("185"));
158         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
159         film->set_name ("frobozz");
160         film->set_interop (true);
161         film->set_sequence (false);
162         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip2.srt"));
163         content->subtitle->set_use (true);
164         content->subtitle->set_burn (false);
165         film->examine_and_add_content (content);
166         film->examine_and_add_content (content);
167         wait_for_jobs ();
168         content->set_position (DCPTime (0));
169         film->make_dcp ();
170         wait_for_jobs ();
171         film->write_metadata ();
172
173         check_dcp ("test/data/xml_subtitle_test2", film->dir (film->dcp_name ()));
174 }
175
176 #if 0
177 /* XXX: this is disabled; there is some difference in font rendering
178    between the test machine and others.
179 */
180
181 /** Test rendering of a SubRip subtitle */
182 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
183 {
184         shared_ptr<Film> film = new_test_film ("subrip_render_test");
185         shared_ptr<TextSubtitleContent> content (new TextSubtitleContent (film, "test/data/subrip.srt"));
186         content->examine (shared_ptr<Job> (), true);
187         BOOST_CHECK_EQUAL (content->full_length(), DCPTime::from_seconds ((3 * 60) + 56.471));
188
189         shared_ptr<SubRipDecoder> decoder (new SubRipDecoder (content));
190         list<ContentTextSubtitle> cts = decoder->get_text_subtitles (
191                 ContentTimePeriod (
192                         ContentTime::from_seconds (109), ContentTime::from_seconds (110)
193                         ), false
194                 );
195         BOOST_CHECK_EQUAL (cts.size(), 1);
196
197         PositionImage image = render_subtitles (cts.front().subs, dcp::Size (1998, 1080));
198         write_image (image.image, "build/test/subrip_render_test.png");
199         check_file ("build/test/subrip_render_test.png", "test/data/subrip_render_test.png");
200 }
201 #endif