OpenShot Library | libopenshot  0.1.1
ExampleBlackmagic.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Main_Blackmagic class (live greenscreen example app)
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include <fstream>
29 #include <iostream>
30 #include <map>
31 #include <queue>
32 #include <tr1/memory>
33 #include "../../include/OpenShot.h"
34 #include <omp.h>
35 #include <time.h>
36 
37 using namespace openshot;
38 
39 int main(int argc, char *argv[])
40 {
41  // Init datetime
42  time_t rawtime;
43  struct tm * timeinfo;
44 
45  /* TIMELINE ---------------- */
46  Timeline t(1920, 1080, Fraction(30,1), 48000, 2, LAYOUT_STEREO);
47 
48  // Create background video
49  ImageReader b1("/home/jonathan/Pictures/moon.jpg");
50  ImageReader b2("/home/jonathan/Pictures/trees.jpg");
51  ImageReader b3("/home/jonathan/Pictures/clouds.jpg");
52  ImageReader b4("/home/jonathan/Pictures/minecraft.png");
53  ImageReader b5("/home/jonathan/Pictures/colorpgg03.jpg");
54  Clip c1(&b1);
55 
56  // Background counter
57  int background_frame = 0;
58  int background_id = 1;
59 
60  DecklinkReader dr(1, 11, 0, 2, 16);
61  Clip c2(&dr);
62  Clip c3(new ImageReader("/home/jonathan/Pictures/watermark.png"));
63 
64  // mask
65  Clip c4(new ImageReader("/home/jonathan/Pictures/mask_small.png"));
66 
67  // CLIP 1 (background image)
68  c1.Position(0.0);
69  c1.scale = SCALE_NONE;
70  c1.Layer(0);
71  t.AddClip(&c1);
72 
73  // CLIP 2 (decklink live stream)
74  c2.Position(0.0);
75  c2.scale = SCALE_NONE;
76  c2.Layer(1);
77  t.AddClip(&c2);
78 
79  // CLIP 3 (foreground image 1)
80  c3.Position(0.0);
81  c3.gravity = GRAVITY_TOP;
82  //c3.gravity = GRAVITY_BOTTOM;
83  c3.scale = SCALE_NONE;
84  c3.Layer(2);
85  t.AddClip(&c3);
86 
87  // CLIP 4 (foreground image 2)
88  c4.Position(0.0);
89  c4.gravity = GRAVITY_TOP;
90  c4.scale = SCALE_NONE;
91  c4.Layer(3);
92  //t.AddClip(&c4);
93 
94  // Decklink writer
95  DecklinkWriter w(0, 11, 3, 2, 16);
96  w.Open();
97 
98  // Loop through reader
99  int x = 0;
100  while (true)
101  {
102  tr1::shared_ptr<Frame> f = t.GetFrame(x);
103  if (f)
104  {
105  if (x != 0 && x % 30 == 0)
106  {
107  cout << "30 frames... (" << abs(dr.GetCurrentFrameNumber() - x) << " diff)" << endl;
108 
109  if (x != 0 && x % 60 == 0)
110  {
111  time ( &rawtime );
112  timeinfo = localtime ( &rawtime );
113 
114  stringstream timestamp;
115  timestamp << asctime (timeinfo);
116 
117  stringstream filename;
118  filename << "/home/jonathan/Pictures/screenshots/detailed/" << timestamp.str() << ".jpeg";
119  f->Save(filename.str(), 1.0);
120  stringstream filename_small;
121  filename_small << "/home/jonathan/Pictures/screenshots/thumbs/" << timestamp.str() << ".jpeg";
122  f->Save(filename_small.str(), 0.15);
123  }
124  }
125 
126  // Send current frame to BlackMagic
127  w.WriteFrame(f);
128 
129  // Increment background frame #
130  background_frame++;
131 
132  // Change background
133  if (background_frame == 300)
134  {
135  background_frame = 0;
136  switch (background_id)
137  {
138  case 1:
139  c1.Reader(&b2);
140  background_id = 2;
141  break;
142  case 2:
143  c1.Reader(&b3);
144  background_id = 3;
145  break;
146  case 3:
147  c1.Reader(&b4);
148  background_id = 4;
149  break;
150  case 4:
151  c1.Reader(&b5);
152  background_id = 5;
153  break;
154  case 5:
155  c1.Reader(&b1);
156  background_id = 1;
157  break;
158  }
159  }
160 
161 
162  //usleep(500 * 1);
163  // Go to next frame on timeline
164  if (abs(dr.GetCurrentFrameNumber() - x) > 40 || x == 90)
165  {
166  // Got behind... skip ahead some
167  x = dr.GetCurrentFrameNumber();
168 
169  cout << "JUMPING AHEAD to " << x << ", background moved to " << (float(x) / 30.0f) << endl;
170  }
171  else
172  // Go to the next frame
173  x++;
174  }
175  }
176 
177  // Sleep
178  sleep(4);
179 
180 
181 
182 
183  // Image Reader
184 // ImageReader r1("/home/jonathan/Pictures/Screenshot from 2013-02-10 15:06:38.png");
185 // r1.Open();
186 // tr1::shared_ptr<Frame> f1 = r1.GetFrame(1);
187 // r1.Close();
188 // f1->TransparentColors("#8fa09a", 20.0);
189 // f1->Display();
190 // return 0;
191 
192 // ImageReader r2("/home/jonathan/Pictures/trees.jpg");
193 // r2.Open();
194 // tr1::shared_ptr<Frame> f2 = r2.GetFrame(1);
195 // r2.Close();
196 
197 // DecklinkReader dr(1, 11, 0, 2, 16);
198 // dr.Open();
199 //
200 // DecklinkWriter w(0, 11, 3, 2, 16);
201 // w.Open();
202 //
203 // // Loop through reader
204 // int x = 0;
205 // while (true)
206 // {
207 // if (x % 30 == 0)
208 // cout << "30 frames..." << endl;
209 //
210 // tr1::shared_ptr<Frame> f = dr.GetFrame(0);
211 // if (f)
212 // {
213 // //f->Display();
214 // w.WriteFrame(f);
215 // usleep(1000 * 1);
216 //
217 // x++;
218 // }
219 // }
220 //
221 // // Sleep
222 // sleep(4);
223 //
224 // // Close writer
225 // w.Close();
226 
227  return 0;
228 }
tr1::shared_ptr< Frame > GetFrame(long int requested_frame)
Definition: Timeline.cpp:589
GravityType gravity
The gravity of a clip determines where it snaps to it's parent.
Definition: Clip.h:150
Do not scale the clip.
Definition: Enums.h:54
unsigned long GetCurrentFrameNumber()
int Layer()
Get layer of clip on timeline (lower number is covered by higher numbers)
Definition: ClipBase.h:78
This class uses the ImageMagick++ libraries, to open image files, and return openshot::Frame objects ...
Definition: ImageReader.h:67
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:108
ScaleType scale
The scale determines how a clip should be resized to fit it's parent.
Definition: Clip.h:151
float Position()
Get position on timeline (in seconds)
Definition: ClipBase.h:77
void Reader(ReaderBase *new_reader)
Set the current reader.
Definition: Clip.cpp:179
This class represents a fraction.
Definition: Fraction.h:42
void WriteFrame(tr1::shared_ptr< Frame > frame)
This method is required for all derived classes of WriterBase. Write a Frame to the video file...
void AddClip(Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:64
void Open()
Open device and video stream - which is called by the constructor automatically.
Align clip to the top center of its parent.
Definition: Enums.h:38
int main(int argc, char *argv[])
This class uses the Blackmagic Decklink libraries, to open video streams on Blackmagic devices...
This class uses the Blackmagic Decklink libraries, to send video streams to Blackmagic devices...
This class represents a timeline.
Definition: Timeline.h:141