Spaces:
Runtime error
Runtime error
File size: 8,685 Bytes
ae8c95b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
<?php
use Monolog\Logger;
class MeltProject
{
private int $width;
private int $height;
private int $frameRateNum;
private int $frameRateDen;
private ?string $audioFile = null;
private string $outputFile;
private array $images;
private Logger $log;
public function __construct(Logger $log, int $width = 1920, int $height = 1080, int $frameRateNum = 25, int $frameRateDen = 1, string $outputFile = './scene.mp4')
{
$this->log = $log;
$this->width = $width;
$this->height = $height;
$this->frameRateNum = $frameRateNum;
$this->frameRateDen = $frameRateDen;
$this->outputFile = $outputFile;
$this->images = [];
$this->log->info('Initialized MeltProject ' . $this);
}
public function __toString() {
return 'width:'.$this->width.' height:'.$this->height . ' framerate:'. $this->frameRateNum . ' outputFile ' . $this->outputFile;
}
public function addImage(string $path, int $in, int $out): void
{
$this->images[] = [
'path' => $path,
'in' => $in,
'out' => $out
];
$this->log->info('Adding image', end($this->images));
}
/**
* Set the voiceover audio track.
*
* @param string $path
*/
public function setVoiceover(string $path): void
{
$this->audioFile = $path;
$this->log->info('Adding audio track: ' . $path);
}
/**
* Generate the XML document.
*
* @return DOMDocument The generated XML object.
*/
public function generateXml(): DOMDocument
{
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$mlt = $this->createMltElement($xml);
$xml->appendChild($mlt);
$profile = $this->createProfileElement($xml);
$mlt->appendChild($profile);
$count = 0;
$playlist = $this->createPlaylistElement($xml);
foreach ($this->images as $image) {
$producer = $this->createProducerElement($xml, $image, $count);
$mlt->appendChild($producer);
$entry = $this->createEntryElement($xml, $image, $count);
$playlist->appendChild($entry);
$count++;
}
$mlt->appendChild($playlist);
$tractor0 = $this->createTractorElement($xml, 'tractor0');
$mlt->appendChild($tractor0);
$multitrack0 = $this->createMultitrackElement($xml);
$tractor0->appendChild($multitrack0);
$imageTrack = $this->createImageTrackElement($xml);
$multitrack0->appendChild($imageTrack);
if ($this->audioFile !== null) {
$voiceoverProducer = $this->createVoiceoverProducerElement($xml, $this->audioFile);
$mlt->appendChild($voiceoverProducer);
$voiceoverPlaylist = $this->createVoiceoverPlaylistElement($xml);
$mlt->appendChild($voiceoverPlaylist);
$voiceoverEntry = $this->createVoiceoverEntryElement($xml);
$voiceoverPlaylist->appendChild($voiceoverEntry);
$tractor1 = $this->createTractorElement($xml, 'tractor1');
$mlt->appendChild($tractor1);
$multitrack1 = $this->createMultitrackElement($xml);
$tractor1->appendChild($multitrack1);
$trackForTractor0 = $this->createTrackElement($xml, 'tractor0');
$multitrack1->appendChild($trackForTractor0);
$trackForVoiceoverPlaylist = $this->createTrackElement($xml, 'voiceover_playlist');
$multitrack1->appendChild($trackForVoiceoverPlaylist);
}
return $xml;
}
private function createMltElement(DOMDocument $xml): DOMElement
{
$mlt = $xml->createElement('mlt');
$mlt->setAttribute('LC_NUMERIC', 'C');
$mlt->setAttribute('producer', 'main_bin');
$mlt->setAttribute('version', '7.12.0');
$mlt->setAttribute('root', '/home/kash');
return $mlt;
}
private function createProfileElement(DOMDocument $xml): DOMElement
{
$profile = $xml->createElement('profile');
$profile->setAttribute('description', 'HD 1080p 25 fps');
$profile->setAttribute('width', $this->width);
$profile->setAttribute('height', $this->height);
$profile->setAttribute('progressive', '1');
$profile->setAttribute('sample_aspect_num', '1');
$profile->setAttribute('sample_aspect_den', '1');
$profile->setAttribute('display_aspect_num', '16');
$profile->setAttribute('display_aspect_den', '9');
$profile->setAttribute('frame_rate_num', $this->frameRateNum);
$profile->setAttribute('frame_rate_den', $this->frameRateDen);
$profile->setAttribute('colorspace', '709');
return $profile;
}
private function createPlaylistElement(DOMDocument $xml): DOMElement
{
$playlist = $xml->createElement('playlist');
$playlist->setAttribute('id', 'playlist0');
return $playlist;
}
private function createProducerElement(DOMDocument $xml, array $image, int $count): DOMElement
{
$producer = $xml->createElement('producer');
$producer->setAttribute('id', 'producer' . $count);
$producer->setAttribute('in', $image['in']);
$producer->setAttribute('out', $image['out']);
$resource = $xml->createElement('property', $image['path']);
$resource->setAttribute('name', 'resource');
$producer->appendChild($resource);
$length = $xml->createElement('property', $image['out'] + 1);
$length->setAttribute('name', 'length');
$producer->appendChild($length);
return $producer;
}
private function createEntryElement(DOMDocument $xml, array $image, int $count): DOMElement
{
$entry = $xml->createElement('entry');
$entry->setAttribute('producer', 'producer' . $count);
$entry->setAttribute('in', $image['in']);
$entry->setAttribute('out', $image['out']);
return $entry;
}
public function createTractorElement(DOMDocument $xml, string $id): DOMElement
{
$tractor = $xml->createElement('tractor');
$tractor->setAttribute('id', $id);
return $tractor;
}
public function createTrackElement(DOMDocument $xml, string $producerId): DOMElement
{
$track = $xml->createElement('track');
$track->setAttribute('producer', $producerId);
return $track;
}
private function createTransitionElement(DOMDocument $xml): DOMElement
{
$transition = $xml->createElement('transition');
$transition->setAttribute('in', '0');
$transition->setAttribute('out', $this->images[0]['out']);
$transition->setAttribute('a_track', '0');
$transition->setAttribute('b_track', '1');
return $transition;
}
private function createTransitionProperties(): array
{
return [
['mlt_service', 'mix'],
['start', '0'],
['end', $this->images[0]['out']],
['a_track', '0'],
['b_track', '1'],
];
}
private function createPropertyElement(DOMDocument $xml, array $property): DOMElement
{
$prop = $xml->createElement('property', $property[1]);
$prop->setAttribute('name', $property[0]);
return $prop;
}
private function createVoiceoverProducerElement(DOMDocument $xml, string $path): DOMElement
{
$producer = $xml->createElement('producer');
$producer->setAttribute('id', 'voiceover');
$resource = $xml->createElement('property', $path);
$resource->setAttribute('name', 'resource');
$producer->appendChild($resource);
return $producer;
}
private function createVoiceoverPlaylistElement(DOMDocument $xml): DOMElement
{
$playlist = $xml->createElement('playlist');
$playlist->setAttribute('id', 'voiceover_playlist');
return $playlist;
}
private function createVoiceoverEntryElement(DOMDocument $xml): DOMElement
{
$entry = $xml->createElement('entry');
$entry->setAttribute('producer', 'voiceover');
return $entry;
}
private function createMultitrackElement(DOMDocument $xml): DOMElement
{
$multitrack = $xml->createElement('multitrack');
return $multitrack;
}
private function createVoiceoverTrackElement(DOMDocument $xml): DOMElement
{
$track = $xml->createElement('track');
$track->setAttribute('producer', 'voiceover_playlist');
return $track;
}
private function createImageTrackElement(DOMDocument $xml): DOMElement
{
$track = $xml->createElement('track');
$track->setAttribute('producer', 'playlist0');
return $track;
}
/**
* Save the MLT project to a file.
*
* @param string $path
* @return bool
*/
public function save(DOMDocument $xml, string $path): bool {
return $xml->save($path) !== false;
}
} |