1+ <?php
2+ /**
3+ * This file is part of PHP-FFmpeg-Extensions library.
4+ * (c) Alexander Sharapov <alexander@sharapov.biz>
5+ * http://sharapov.biz/
6+ */
7+
8+ use \Sharapov \FFMpegExtensions \Input \File as InputFile ;
9+ use \Sharapov \FFMpegExtensions \Filters \Video \FilterComplexOptions ;
10+ use \Sharapov \FFMpegExtensions \Coordinate ;
11+ use \Alchemy \BinaryDriver \Exception \ExecutionFailureException ;
12+
13+ require_once 'init.php ' ;
14+
15+ // Open source video
16+ $ video = $ ffmpeg ->open (new InputFile ('source/spokesperson-alpha.mov ' ));
17+
18+ // Initiate complex filter options collection
19+ $ options = new FilterComplexOptions \OptionsCollection ();
20+
21+ // Create alphakey option
22+ $ alphaKey = new FilterComplexOptions \OptionAlphakey ();
23+ $ alphaKey
24+ // Set input stream for alphakey
25+ ->setExtraInputStream (new InputFile ('source/demo_video_720p_HD.mp4 ' ))
26+ ->setDimensions (new Coordinate \Dimension (1280 , 720 ))
27+ // Set video alphakey background duration to meet the main video duration
28+ ->setDuration (new Coordinate \Duration ($ video ->getStreamDuration ()));
29+
30+ $ options
31+ ->add ($ alphaKey );
32+
33+ // Pass option to the options collection
34+ $ options
35+ ->add (((new FilterComplexOptions \OptionDrawText ())
36+ // Set z-index property. Greater value is always in front
37+ ->setZIndex (160 )
38+ // You can use fade-in and fade-out effects. Set time in seconds
39+ ->setFadeIn (1 )
40+ ->setFadeOut (1 )
41+ // Set font path
42+ ->setFontFile (new InputFile ('source/arial.ttf ' ))
43+ // Set font color. Accepts transparency value as the second argument. Float value between 0 and 1.
44+ ->setFontColor ('ffffff ' )
45+ // Set font size in pixels
46+ ->setFontSize (33 )
47+ // Set text string
48+ ->setText ('Spokesperson chromakey demonstration ' )
49+ // Coordinates where the text should be rendered. Accepts positive integer or
50+ // constants "(w-tw)/2", "(h-th)/2" to handle auto-horizontal, auto-vertical values
51+ ->setCoordinates (new Coordinate \Point (Coordinate \Point::AUTO_HORIZONTAL , 50 ))
52+ // Set timings (start, stop) in seconds. Accepts float values as well
53+ ->setTimeLine (new Coordinate \TimeLine (1 , 6 ))
54+ // Set bounding box
55+ ->setBoundingBox ('000000 ' , 10 , 0.5 )));
56+
57+ // Apply filter options to video
58+ $ video
59+ ->filters ()
60+ ->complex ($ options );
61+
62+ // Run render
63+ $ format = new \FFMpeg \Format \Video \X264 ('libmp3lame ' );
64+ $ format ->on ('progress ' , function ($ video , $ format , $ percentage ) {
65+ echo "$ percentage% transcoded \n" ;
66+ });
67+
68+ try {
69+ $ video
70+ ->save ($ format , 'output/output.mp4 ' );
71+ print 'Done! ' ;
72+ } catch (ExecutionFailureException $ e ) {
73+ print $ e ->getMessage ();
74+ }
0 commit comments