MLT++ library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

474 lines
15 KiB

  1. INTRODUCTION
  2. ------------
  3. This document provides a brief tutorial on the use of the mlt++ wrapper
  4. and bindings.
  5. Hello World
  6. -----------
  7. The mlt++ wrapper is a c++ wrapper for the mlt C library. As such, it
  8. provides clean C++ access to the underlying library.
  9. An example of use is as follows:
  10. #include <mlt++/Mlt.h>
  11. using namespace Mlt;
  12. int main( void )
  13. {
  14. Factory::init( );
  15. Producer p( "pango:", "Hello World" );
  16. Consumer c( "sdl" );
  17. c.connect( p );
  18. c.run( );
  19. return 0;
  20. }
  21. This is a fairly typical example of mlt++ usage - create a 'producer' (an
  22. object which produces 'frames'), create a 'consumer' (an object which consumes
  23. frames), connect them together, start the consumer and wait until done (here
  24. we just wait for the user to close the window).
  25. In this case, we construct a window as a consumer using the 'sdl' consumer
  26. (SDL is a standard portable library which provides platform independent
  27. access to accelerated video display and audio) and use the 'pango'
  28. producer to generate frames with the words 'Hello World' (pango is a
  29. library from the gtk toolkit).
  30. The main point of this example is to show that mlt uses existing libraries
  31. to provide its functionality - this keeps the framework itself very small.
  32. Note that mlt is designed to be housed in GUI or server type applications -
  33. typically, applications don't wait around for the consumer to be stopped in
  34. the manner shown.
  35. So far, we've introduced the Producer and Consumer mlt classes. We'll cover
  36. each of these in more detail later in the tutorial, but for now, we'll
  37. briefly cover the remaining classes.
  38. Playlists
  39. ---------
  40. Another simple class is the Playlist - this is direct extension of Producer
  41. and it allows you to maintain a list of producer objects.
  42. As a simple example of the Playlist in action, we'll convert the example
  43. above into an application which plays multiple video or audio files.
  44. #include <mlt++/Mlt.h>
  45. using namespace Mlt;
  46. int main( int argc, char **argv )
  47. {
  48. Factory::init( );
  49. Playlist list;
  50. for ( int i = 1; i < argc; i ++ )
  51. {
  52. Producer p( argv[i] );
  53. if ( p.is_valid( ) )
  54. list.append( p );
  55. }
  56. Consumer c( "sdl" );
  57. c.connect( list );
  58. c.run( );
  59. return 0;
  60. }
  61. Now you can run the program as:
  62. ./player *.avi *.mp3 *.jpg etc
  63. In this case, we construct a playlist by simply appending producers to it.
  64. Notice that although the scope of the Producer is limited to the inner
  65. for loop, we can safely add it to the playlist - this is due to the fact
  66. that all mlt objects maintain reference counts and no object is really
  67. destroyed until all the references are gone. In this case, when the list
  68. object goes out of scope, all the producers we created will automatically
  69. be destroyed.
  70. Filters
  71. -------
  72. So far, we've shown how you can load and play media. We've given a brief
  73. intro to the Playlist container, now it's time to start manipulating
  74. things...
  75. For the next example, I'll add a 'watermark' to the video - a watermark
  76. is used by broadcasters to brand the channel and normally consists of a
  77. logo of some sort. We'll just use some black text on a partially
  78. transparent red background.
  79. #include <mlt++/Mlt.h>
  80. using namespace Mlt;
  81. int main( int argc, char **argv )
  82. {
  83. Factory::init( );
  84. Playlist list;
  85. for ( int i = 1; i < argc; i ++ )
  86. {
  87. Producer p( argv[i] );
  88. if ( p.is_valid( ) )
  89. list.append( p );
  90. }
  91. Filter f( "watermark", "pango:" );
  92. f.set( "producer.text", "MLT++" );
  93. f.set( "producer.fgcolour", "0x000000ff" );
  94. f.set( "producer.bgcolour", "0xff000080" );
  95. list.attach( f );
  96. Consumer c( "sdl" );
  97. c.connect( list );
  98. c.run( );
  99. return 0;
  100. }
  101. Notice that the watermark filter reuses the 'pango' producer we showed in the
  102. first example. In fact, you could use any producer here - if you wanted to
  103. use a graphic or a video, you would just construct the filter with a full path
  104. to that as the second argument.
  105. We manipulate the filter using the set method - this method was also shown
  106. in the first example.
  107. Finally, we attach the filter to the playlist. This ensure that all frames
  108. that are obtained from the playlist are watermarked.
  109. Cuts
  110. ----
  111. When you add a clip to a playlist, the a cut object is created - this is merely a
  112. wrapper for the producer, spanning the specified in and out points.
  113. Whenever you retrieve a clip from a playlist, you will always get a cut object.
  114. This allows you to attach filters to a specific part of a producer and should
  115. the position of the cut in the playlist change, then the filter will remain
  116. correctly associated to it.
  117. A producer and a cut are generally identical in behaviour, but should you need to
  118. distinguish between them, you can use:
  119. if ( producer.is_cut( ) )
  120. and to retrieve the parent of a cut, you can use:
  121. Producer parent = producer.parent_cut( );
  122. Filters that are attached directly to a parent are executed before any filters
  123. attached to the cut.
  124. Tractor
  125. -------
  126. A tractor is an object that allows the manipulation of multiple video and audio
  127. tracks.
  128. Stepping away from the player example we've been tinkering with for a minute,
  129. let's assume we want to do something like dub a video with some audio. This
  130. a very trivial thing to do:
  131. Tractor *dub( char *video_file, char *audio_file )
  132. {
  133. Tractor *tractor = new Tractor( );
  134. Producer video( video_file );
  135. Producer audio( audio_file );
  136. tractor->set_track( video, 0 );
  137. tractor->set_track( audio, 1 );
  138. return tractor;
  139. }
  140. That's all that needs to be done - you can now connect the returned object to a
  141. consumer, or add it to a playlist, or even apply it as a track to another tractor.
  142. Transition
  143. ----------
  144. Let's now assume we want to mix the audio between two tracks - to do this, we
  145. need to introduce the concept of a transition. A transition in mlt is a service
  146. which combines frames from two producers to produce a new frame.
  147. Tractor *mix( char *video_file, char *audio_file )
  148. {
  149. Tractor *tractor = new Tractor( );
  150. Transition mix( "mix" );
  151. Producer video( video_file );
  152. Producer audio( audio_file );
  153. tractor.set_track( video, 0 );
  154. tractor.set_track( audio, 1 );
  155. tractor.field.plant_transition( mix, 0, 1 );
  156. return tractor;
  157. }
  158. The tractor returned will now mix the audio from the original video and the
  159. audio.
  160. Mix
  161. ---
  162. There is a convenience function which simplifies the process of applying
  163. transitions betwee adjacent cuts on a playlist. This is often preferable
  164. to use over the constuction of your own tractor and transition set up.
  165. To apply a 25 frame luma transition between the first and second cut on
  166. the playlist, you could use:
  167. Transition luma;
  168. playlist.mix( 0, 25, luma );
  169. Events
  170. ------
  171. Typically, applications need to be informed when changes occur in an mlt++ object.
  172. This facilitates application services such as undo/redo management, or project
  173. rendering in a timeline type widget and many other types of operations which an
  174. application needs.
  175. As an example, consider the following:
  176. class Westley
  177. {
  178. private:
  179. Consumer consumer;
  180. Tractor &tractor;
  181. public:
  182. Westley( MltTractor &tractor ) :
  183. tractor( tractor ),
  184. consumer( "westley" )
  185. {
  186. consumer.connect( tractor );
  187. tractor.listen( tractor, "producer-changed",
  188. ( mlt_listener )Westley::listener );
  189. }
  190. static void listener( Properties *tractor, Westley *object )
  191. {
  192. object->activate( );
  193. }
  194. void activate( )
  195. {
  196. consumer.start( );
  197. }
  198. };
  199. Now, each time the tractor is changed, the westley representation is output to
  200. stderr.
  201. Servers and Westley Docs
  202. ------------------------
  203. One of the key features of MLT is its server capabilities. This feature
  204. allows you to pass westley documents seamlessly from one process to
  205. another and even to different computers on your network.
  206. The miracle playout server is one such example of an application which
  207. uses this functionality - you can build your own servers into your own
  208. processes with ease.
  209. A server process would be running as follows:
  210. #include <mlt++/Miracle>
  211. using namespace Mlt;
  212. int main( void )
  213. {
  214. Miracle miracle( "miracle", 5250 );
  215. miracle.start( );
  216. miracle.execute( "uadd sdl" );
  217. miracle.execute( "play u0" );
  218. miracle.wait_for_shutdown( );
  219. return 0;
  220. }
  221. Typically, when you have an MLT object such as a producer or a playlist,
  222. you can send a westley representation of this to a running server with:
  223. Conumser valerie( "valerie", "localhost:5250" );
  224. valerie.connect( producer );
  225. valerie.start( );
  226. The effect of the push will be to append the producer on to the first
  227. unit (u0).
  228. You can completely customise the miracle server - an example of this
  229. is shown below.
  230. That's All Folks...
  231. -------------------
  232. And that, believe it or not, is a fairly complete summary of the classes you'll
  233. typically be interfacing with in mlt++. Obviously, there's a little more to it
  234. than this - a couple of intrisinc classes have been glossed over (notably, the
  235. Properties and Service base classes). The next section will cover all of the
  236. above, but in much more detail...
  237. DIGGING DEEPER
  238. --------------
  239. The previous section was designed to give you a whistle stop tour through the major
  240. framework classes. This section will take you through the scenic route.
  241. Introducing Base Classes
  242. ------------------------
  243. Services in mlt are the collective noun for Producers, Filters, Transitions and
  244. Consumer. A Service is also the base class from which all of these classes
  245. extend. It provides the basic connectivity which has been shown throughout the
  246. examples in the previous section.
  247. Properties are the main way in which we communicate with the Services -
  248. essentially, it provides get/set methods for named values. All services extend
  249. Properties.
  250. Properties
  251. ----------
  252. Properties provide the general mechanism for communicating with Services -
  253. through the Properties interface, we are able to manipulate and serialise
  254. a services state.
  255. For example, to dump all the properties to stdout, you can use something
  256. like:
  257. void dump( Properties &properties )
  258. {
  259. for ( int i = 0; i < properties.count( ); i ++ )
  260. cout << Properties.get_name( i ) << " = " << Properties.get( i ) << endl;
  261. }
  262. Note that the properties object handles type conversion, so the following
  263. is acceptable:
  264. properties.set( "hello", "10.5" );
  265. int hello_int = properties.get_int( "hello" );
  266. double hello_double = properties.get_double( "hello" );
  267. A couple of convenience methods are provide to examine or serialise property
  268. objects.
  269. For example:
  270. properties.debug( );
  271. will report all serialisable properties on stderr, in the form:
  272. Object: [ ref=1, in=0, out=0, track=0, u=75, v=150, _unique_id=15,
  273. mlt_type=filter, mlt_service=sepia ]
  274. Services
  275. --------
  276. Typically, all the services are constructed via the specific classes
  277. constructor. Often, you will receive Service objects rather than their
  278. specific type. In order to access the extended classes interface,
  279. you will need to create a reference.
  280. For example, given an arbitrary Service object, you can determine its
  281. type by using the type method - this will return a 'service_type' which
  282. has values of producer_type, filter_type etc. Alternatively, you can
  283. create a wrapping object and check on its validity.
  284. bool do_we_have_a_producer( Service &service )
  285. {
  286. Producer producer( service );
  287. return producer.is_valid( );
  288. }
  289. Events
  290. ------
  291. Servers and Westley Docs
  292. ------------------------
  293. For various reasons, you might want to serialise a producer to a string.
  294. To do this, you just need to specify a property to write to:
  295. Consumer westley( "westley", "buffer" );
  296. westley.connect( producer );
  297. westley.start( );
  298. buffer = westley.get( "buffer" );
  299. You can use any name you want, and you can change it using the "resource"
  300. property. Any name with a '.' in it is considered to be a file. Hence, you
  301. can use a westley consumer to store multiple instances of the same MLT
  302. object - useful if you want to provide undo/redo capabilities in an
  303. editing application.
  304. Should you receive an xml document as a string, and you want to send it
  305. on to a server, you can use:
  306. Conumser valerie( "valerie", "localhost:5250" );
  307. valerie.set( "westley", buffer );
  308. valerie.start( );
  309. If you need to obtain an MLT object from a string:
  310. Producer producer( "westley-xml", buffer );
  311. The following shows a working example of an extended server:
  312. class ShotcutServer : public Miracle
  313. {
  314. public:
  315. ShotcutServer( char *id, int port ) :
  316. Miracle( id, port )
  317. {
  318. }
  319. void set_receive_doc( bool doc )
  320. {
  321. set( "push-parser-off", doc );
  322. }
  323. // Reject all commands other than push/receive
  324. Response *execute( char *command )
  325. {
  326. valerie_response response = valerie_response_init( );
  327. valerie_response_set_error( response, 400, "Not OK" );
  328. return new Response( response );
  329. }
  330. // Push document handler
  331. Response *received( char *command, char *doc )
  332. {
  333. valerie_response response = valerie_response_init( );
  334. // Use doc in some way and assign Response
  335. if ( doc != NULL )
  336. valerie_response_set_error( response, 200, "OK" );
  337. return new Response( response );
  338. }
  339. // Push service handler
  340. Response *push( char *command, Service *service )
  341. {
  342. valerie_response response = valerie_response_init( );
  343. // Use service in some way and assign Response
  344. if ( service != NULL )
  345. valerie_response_set_error( response, 200, "OK" );
  346. return new Response( response );
  347. }
  348. };
  349. NB: Should you be incorporating this into a GUI application, remember that the
  350. execute, received and push methods are invoked from a thread - make sure that
  351. you honour the locking requirements of your GUI toolkit before interacting with
  352. the UI.