OpenShot Library | libopenshot  0.2.7
Caption.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for Caption effect class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #ifndef OPENSHOT_CAPTION_EFFECT_H
32 #define OPENSHOT_CAPTION_EFFECT_H
33 
34 #include <memory>
35 #include <string>
36 #include <vector>
37 #include <QFont>
38 #include <QFontMetrics>
39 #include <QRegularExpression>
40 #include "../Color.h"
41 #include "../EffectBase.h"
42 #include "../Json.h"
43 #include "../KeyFrame.h"
44 
45 
46 
47 namespace openshot
48 {
49 
50 /**
51  * @brief This class adds captions/text over a video, based on timestamps. You can also animate some limited
52  * aspects, such as words appearing/disappearing.
53  *
54  * Adding captions can be an easy way to generate text overlays through-out a long clip.
55  */
56 class Caption : public EffectBase
57 {
58 private:
59  std::vector<QRegularExpressionMatch> matchedCaptions; ///< RegEx to capture cues and text
60  std::string caption_text; ///< Text of caption
61  QFontMetrics* metrics; ///< Font metrics object
62  QFont* font; ///< QFont object
63  bool is_dirty;
64 
65  /// Init effect settings
66  void init_effect_details();
67 
68  /// Process regex capture
69  void process_regex();
70 
71 
72 public:
73  Color color; ///< Color of caption text
74  Color stroke; ///< Color of text border / stroke
75  Color background; ///< Color of caption area background
76  Keyframe background_alpha; ///< Background color alpha
77  Keyframe background_corner; ///< Background cornder radius
78  Keyframe background_padding; ///< Background padding
79  Keyframe stroke_width; ///< Width of text border / stroke
80  Keyframe font_size; ///< Font size in points
81  Keyframe font_alpha; ///< Font color alpha
82  Keyframe left; ///< Size of left bar
83  Keyframe top; ///< Size of top bar
84  Keyframe right; ///< Size of right bar
85  Keyframe fade_in; ///< Fade in per caption (# of seconds)
86  Keyframe fade_out; ///< Fade in per caption (# of seconds)
87  std::string font_name; ///< Font string
88 
89  /// Blank constructor, useful when using Json to load the effect properties
90  Caption();
91 
92  /// Default constructor, which takes a string of VTT/Subrip formatted caption data, and displays them over time.
93  ///
94  /// @param captions A string with VTT/Subrip format text captions
95  Caption(std::string captions);
96 
97  /// @brief This method is required for all derived classes of ClipBase, and returns a
98  /// new openshot::Frame object. All Clip keyframes and effects are resolved into
99  /// pixels.
100  ///
101  /// @returns A new openshot::Frame object
102  /// @param frame_number The frame number (starting at 1) of the clip or effect on the timeline.
103  std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number) override { return GetFrame(std::make_shared<openshot::Frame>(), frame_number); }
104 
105  /// @brief This method is required for all derived classes of ClipBase, and returns a
106  /// modified openshot::Frame object
107  ///
108  /// The frame object is passed into this method and used as a starting point (pixels and audio).
109  /// All Clip keyframes and effects are resolved into pixels.
110  ///
111  /// @returns The modified openshot::Frame object
112  /// @param frame The frame object that needs the clip or effect applied to it
113  /// @param frame_number The frame number (starting at 1) of the clip or effect on the timeline.
114  std::shared_ptr<openshot::Frame> GetFrame(std::shared_ptr<openshot::Frame> frame, int64_t frame_number) override;
115 
116  // Get and Set caption data
117  std::string CaptionText(); ///< Set the caption string to use (see VTT format)
118  void CaptionText(std::string new_caption_text); ///< Get the caption string
119 
120  // Get and Set JSON methods
121  std::string Json() const override; ///< Generate JSON string of this object
122  void SetJson(const std::string value) override; ///< Load JSON string into this object
123  Json::Value JsonValue() const override; ///< Generate Json::Value for this object
124  void SetJsonValue(const Json::Value root) override; ///< Load Json::Value into this object
125 
126  /// Get all properties for a specific frame (perfect for a UI to display the current state
127  /// of all properties at any time)
128  std::string PropertiesJSON(int64_t requested_frame) const override;
129 };
130 
131 } // namespace openshot
132 
133 #endif // OPENSHOT_CAPTION_EFFECT_H
This class adds captions/text over a video, based on timestamps. You can also animate some limited as...
Definition: Caption.h:57
std::string PropertiesJSON(int64_t requested_frame) const override
Definition: Caption.cpp:394
Keyframe background_padding
Background padding.
Definition: Caption.h:78
Caption()
Blank constructor, useful when using Json to load the effect properties.
Definition: Caption.cpp:45
Keyframe stroke_width
Width of text border / stroke.
Definition: Caption.h:79
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: Caption.cpp:306
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: Caption.cpp:350
std::string Json() const override
Generate JSON string of this object.
Definition: Caption.cpp:299
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: Caption.cpp:333
std::string font_name
Font string.
Definition: Caption.h:87
Color background
Color of caption area background.
Definition: Caption.h:75
Keyframe font_size
Font size in points.
Definition: Caption.h:80
Keyframe font_alpha
Font color alpha.
Definition: Caption.h:81
Keyframe background_alpha
Background color alpha.
Definition: Caption.h:76
Keyframe fade_out
Fade in per caption (# of seconds)
Definition: Caption.h:86
Keyframe background_corner
Background cornder radius.
Definition: Caption.h:77
Keyframe fade_in
Fade in per caption (# of seconds)
Definition: Caption.h:85
Keyframe top
Size of top bar.
Definition: Caption.h:83
Color stroke
Color of text border / stroke.
Definition: Caption.h:74
std::string CaptionText()
Set the caption string to use (see VTT format)
Definition: Caption.cpp:83
Keyframe right
Size of right bar.
Definition: Caption.h:84
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number) override
This method is required for all derived classes of ClipBase, and returns a new openshot::Frame object...
Definition: Caption.h:103
Color color
Color of caption text.
Definition: Caption.h:73
Keyframe left
Size of left bar.
Definition: Caption.h:82
This class represents a color (used on the timeline and clips)
Definition: Color.h:45
This abstract class is the base class, used by all effects in libopenshot.
Definition: EffectBase.h:71
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:72
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47