2899d5064cd5dcb3dd2918c8e11074019c9c0a0b
[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  */
24
25 #include "lib/film.h"
26 #include "lib/text_subtitle_content.h"
27 #include "lib/dcp_content_type.h"
28 #include "lib/font.h"
29 #include "lib/ratio.h"
30 #include "lib/subtitle_content.h"
31 #include "test.h"
32 #include <boost/test/unit_test.hpp>
33 #include <boost/algorithm/string.hpp>
34 #include <boost/make_shared.hpp>
35 #include <list>
36
37 using std::string;
38 using std::list;
39 using boost::shared_ptr;
40 using boost::make_shared;
41
42 /** Make a very short DCP with a single subtitle from .srt with no specified fonts */
43 BOOST_AUTO_TEST_CASE (srt_subtitle_test)
44 {
45         shared_ptr<Film> film = new_test_film ("srt_subtitle_test");
46         film->set_container (Ratio::from_id ("185"));
47         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
48         film->set_name ("frobozz");
49         shared_ptr<TextSubtitleContent> content = make_shared<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         shared_ptr<TextSubtitleContent> content = make_shared<TextSubtitleContent> (film, "test/data/subrip2.srt");
70         film->examine_and_add_content (content);
71         wait_for_jobs ();
72
73         content->subtitle->set_use (true);
74         content->subtitle->set_burn (false);
75         /* Use test/data/subrip2.srt as if it were a font file  */
76         content->subtitle->fonts().front()->set_file (FontFiles::NORMAL, "test/data/subrip2.srt");
77
78         film->make_dcp ();
79         wait_for_jobs ();
80
81         /* Should be blank video with a subtitle MXF */
82         check_dcp ("test/data/srt_subtitle_test2", film->dir (film->dcp_name ()));
83 }
84
85 /** Make another DCP with a longer .srt file */
86 BOOST_AUTO_TEST_CASE (srt_subtitle_test3)
87 {
88         shared_ptr<Film> film = new_test_film ("srt_subtitle_test3");
89
90         film->set_container (Ratio::from_id ("185"));
91         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TLR"));
92         film->set_name ("frobozz");
93         film->set_interop (true);
94         shared_ptr<TextSubtitleContent> content = make_shared<TextSubtitleContent> (film, private_data / "Ankoemmling.srt");
95         film->examine_and_add_content (content);
96         wait_for_jobs ();
97
98         content->subtitle->set_use (true);
99         content->subtitle->set_burn (false);
100
101         film->make_dcp ();
102         wait_for_jobs ();
103
104         /* Find the subtitle file and check it */
105         for (
106                 boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (film->directory() / film->dcp_name (false));
107                 i != boost::filesystem::directory_iterator ();
108                 ++i) {
109
110                 if (boost::filesystem::is_directory (i->path ())) {
111                         for (
112                                 boost::filesystem::directory_iterator j = boost::filesystem::directory_iterator (i->path ());
113                                 j != boost::filesystem::directory_iterator ();
114                                 ++j) {
115
116                                 if (boost::algorithm::starts_with (j->path().leaf().string(), "sub_")) {
117                                         list<string> ignore;
118                                         ignore.push_back ("SubtitleID");
119                                         check_xml (*j, private_data / "Ankoemmling.xml", ignore);
120                                 }
121                         }
122                 }
123         }
124 }
125
126 #if 0
127 /* XXX: this is disabled; there is some difference in font rendering
128    between the test machine and others.
129 */
130
131 /** Test rendering of a SubRip subtitle */
132 BOOST_AUTO_TEST_CASE (srt_subtitle_test4)
133 {
134         shared_ptr<Film> film = new_test_film ("subrip_render_test");
135         shared_ptr<TextSubtitleContent> content = make_shared<TextSubtitleContent> (film, "test/data/subrip.srt");
136         content->examine (shared_ptr<Job> (), true);
137         BOOST_CHECK_EQUAL (content->full_length(), DCPTime::from_seconds ((3 * 60) + 56.471));
138
139         shared_ptr<SubRipDecoder> decoder = make_shared<SubRipDecoder> (content);
140         list<ContentTextSubtitle> cts = decoder->get_text_subtitles (
141                 ContentTimePeriod (
142                         ContentTime::from_seconds (109), ContentTime::from_seconds (110)
143                         ), false
144                 );
145         BOOST_CHECK_EQUAL (cts.size(), 1);
146
147         PositionImage image = render_subtitles (cts.front().subs, dcp::Size (1998, 1080));
148         write_image (image.image, "build/test/subrip_render_test.png");
149         check_file ("build/test/subrip_render_test.png", "test/data/subrip_render_test.png");
150 }
151 #endif