scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 namespace Tests\FFMpeg\Functional;
4  
5 use FFMpeg\Coordinate\Dimension;
6 use FFMpeg\Filters\Video\ResizeFilter;
7 use FFMpeg\Filters\Video\RotateFilter;
8 use FFMpeg\Format\Video\X264;
9 use FFMpeg\Media\Video;
10  
11 class VideoTranscodeTest extends FunctionalTestCase
12 {
13 public function testSimpleTranscodeX264()
14 {
15 $filename = __DIR__ . '/output/output-x264.mp4';
16 if (is_file($filename)) {
17 unlink(__DIR__ . '/output/output-x264.mp4');
18 }
19  
20 $ffmpeg = $this->getFFMpeg();
21 $video = $ffmpeg->open(__DIR__ . '/../files/Test.ogv');
22  
23 $this->assertInstanceOf('FFMpeg\Media\Video', $video);
24  
25 $lastPercentage = null;
26 $phpunit = $this;
27  
28 $codec = new X264('aac');
29 $codec->on('progress', function ($video, $codec, $percentage) use ($phpunit, &$lastPercentage) {
30 if (null !== $lastPercentage) {
31 $phpunit->assertGreaterThanOrEqual($lastPercentage, $percentage);
32 }
33 $lastPercentage = $percentage;
34 $phpunit->assertGreaterThanOrEqual(0, $percentage);
35 $phpunit->assertLessThanOrEqual(100, $percentage);
36 });
37  
38 $video->save($codec, $filename);
39 $this->assertFileExists($filename);
40 unlink($filename);
41 }
42  
43 public function testAacTranscodeX264()
44 {
45 $filename = __DIR__ . '/output/output-x264_2.mp4';
46 if (is_file($filename)) {
47 unlink(__DIR__ . '/output/output-x264_2.mp4');
48 }
49  
50 $ffmpeg = $this->getFFMpeg();
51 $video = $ffmpeg->open(__DIR__ . '/../files/sample.3gp');
52  
53 $this->assertInstanceOf('FFMpeg\Media\Video', $video);
54  
55 $lastPercentage = null;
56 $phpunit = $this;
57  
58 $codec = new X264('aac');
59 $codec->on('progress', function ($video, $codec, $percentage) use ($phpunit, &$lastPercentage) {
60 if (null !== $lastPercentage) {
61 $phpunit->assertGreaterThanOrEqual($lastPercentage, $percentage);
62 }
63 $lastPercentage = $percentage;
64 $phpunit->assertGreaterThanOrEqual(0, $percentage);
65 $phpunit->assertLessThanOrEqual(100, $percentage);
66 });
67  
68 $video->save($codec, $filename);
69 $this->assertFileExists($filename);
70 unlink($filename);
71 }
72  
73 /**
74 * @expectedException \FFMpeg\Exception\RuntimeException
75 */
76 public function testTranscodeInvalidFile()
77 {
78 $ffmpeg = $this->getFFMpeg();
79 $ffmpeg->open(__DIR__ . '/../files/UnknownFileTest.ogv');
80 }
81  
82 public function testSaveInvalidForgedVideo()
83 {
84 $ffmpeg = $this->getFFMpeg();
85 $video = new Video(__DIR__ . '/../files/UnknownFileTest.ogv', $ffmpeg->getFFMpegDriver(), $ffmpeg->getFFProbe());
86  
87 $this->setExpectedException('FFMpeg\Exception\RuntimeException');
88 $video->save(new X264('aac'), __DIR__ . '/output/output-x264.mp4');
89 }
90  
91 public function testTranscodePortraitVideo()
92 {
93 $info = $this->getNameAndVersion();
94  
95 if ($info['name'] === 'avconv' && version_compare($info['version'], '0.9', '<')) {
96 $this->markTestSkipped('This version of avconv is buggy and does not support this test.');
97 }
98  
99 $filename = __DIR__ . '/output/output-x264.mp4';
100 if (is_file($filename)) {
101 unlink(__DIR__ . '/output/output-x264.mp4');
102 }
103  
104 $ffmpeg = $this->getFFMpeg();
105 $video = $ffmpeg->open(__DIR__ . '/../files/portrait.MOV');
106  
107 $video->filters()
108 ->resize(new Dimension(320, 240), ResizeFilter::RESIZEMODE_INSET)
109 ->rotate(RotateFilter::ROTATE_90);
110 $video->save(new X264('aac'), $filename);
111  
112 $dimension = $ffmpeg->getFFProbe()
113 ->streams($filename)
114 ->videos()
115 ->first()
116 ->getDimensions();
117  
118 $this->assertLessThan(1, $dimension->getRatio(false)->getValue());
119 $this->assertEquals(240, $dimension->getHeight());
120  
121 $this->assertFileExists($filename);
122 unlink($filename);
123 }
124  
125 private function getNameAndVersion()
126 {
127 $binary = $this
128 ->getFFMpeg()
129 ->getFFMpegDriver()
130 ->getProcessBuilderFactory()
131 ->getBinary();
132  
133 $output = $matches = null;
134 exec($binary . ' -version 2>&1', $output);
135  
136 if (!isset($output[0])) {
137 return array('name' => null, 'version' => null);
138 }
139  
140 preg_match('/^([a-z]+)\s+version\s+([0-9\.]+)/i', $output[0], $matches);
141  
142 if (count($matches) > 0) {
143 return array('name' => $matches[1], 'version' => $matches[2]);
144 }
145  
146 return array('name' => null, 'version' => null);
147 }
148 }