Completed
Push — master ( edc99c...2b35d2 )
by Victor
10:12
created

Movie::generateFromMovie()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 2
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Georg Ehrke <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 * @author Morris Jobke <[email protected]>
6
 * @author Olivier Paroz <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 * @author Lorenzo Perone <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2017, ownCloud GmbH
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
namespace OC\Preview;
27
28
class Movie extends Provider {
29
	public static $avconvBinary;
30
	public static $ffmpegBinary;
31
	public static $atomicParsleyBinary;
32
33
	/**
34
	 * Keep track of movies without artwork to avoid retries in same request
35
	 * @var array
36
	 */
37
	private $noArtworkIndex = array();
38
39
	/**
40
	 * {@inheritDoc}
41
	 */
42
	public function getMimeType() {
43
		return '/video\/.*/';
44
	}
45
46
	/**
47
	 * {@inheritDoc}
48
	 */
49
	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
50
		// TODO: use proc_open() and stream the source file ?
51
52
		$fileInfo = $fileview->getFileInfo($path);
53
		$useFileDirectly = (!$fileInfo->isEncrypted() && !$fileInfo->isMounted());
54
55
		if ($useFileDirectly) {
56
			$absPath = $fileview->getLocalFile($path);
57 View Code Duplication
		} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
			$absPath = \OC::$server->getTempManager()->getTemporaryFile();
59
60
			$handle = $fileview->fopen($path, 'rb');
61
62
			// we better use 5MB (1024 * 1024 * 5 = 5242880) instead of 1MB.
63
			// in some cases 1MB was no enough to generate thumbnail
64
			$firstmb = stream_get_contents($handle, 5242880);
65
			file_put_contents($absPath, $firstmb);
66
		}
67
68
		$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
69
		if ($result === false) {
70
			$result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
71
			if ($result === false) {
72
				$result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
73
			}
74
		}
75
76
		if (!$useFileDirectly) {
77
			unlink($absPath);
78
		}
79
80
		return $result;
81
	}
82
83
	/**
84
	 * @param $absPath
85
	 * @return bool|string
86
	 */
87
	private function extractMp4CoverArtwork($absPath) {
88
		if (isset($this->noArtworkIndex[$absPath])) {
89
			return false;
90
		}
91
92
		if (self::$atomicParsleyBinary) {
93
			$suffix = substr($absPath, -4);
94
			if ('.mp4' === strtolower($suffix)) {
95
				$tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
96
				$tmpBase = $tmpFolder.'/Cover';
97
				$cmd = self::$atomicParsleyBinary . ' ' .
98
					escapeshellarg($absPath).
99
					' --extractPixToPath ' . escapeshellarg($tmpBase) .
100
					' > /dev/null 2>&1';
101
102
				exec($cmd, $output, $returnCode);
103
104
				if ($returnCode === 0) {
105
					$endings = array('.jpg', '.png');
106
					foreach ($endings as $ending) {
107
						$extractedFile = $tmpBase.'_artwork_1'.$ending;
108
						if (is_file($extractedFile) &&
109
							filesize($extractedFile) > 0) {
110
							return $extractedFile;
111
						}
112
					}
113
				}
114
			}
115
		}
116
		$this->noArtworkIndex[$absPath] = true;
117
		return false;
118
	}
119
120
	/**
121
	 * @param $absPath
122
	 * @param $second
123
	 * @return bool|string
124
	 */
125
	private function generateFromMovie($absPath, $second) {
126
		$tmpPath = \OC::$server->getTempManager()->getTemporaryFile();
127
128
		if (self::$avconvBinary) {
129
			$cmd = self::$avconvBinary . ' -y -ss ' . escapeshellarg($second) .
130
				' -i ' . escapeshellarg($absPath) .
131
				' -an -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) .
132
				' > /dev/null 2>&1';
133
		} else {
134
			$cmd = self::$ffmpegBinary . ' -y -ss ' . escapeshellarg($second) .
135
				' -i ' . escapeshellarg($absPath) .
136
				' -f mjpeg -vframes 1' .
137
				' ' . escapeshellarg($tmpPath) .
138
				' > /dev/null 2>&1';
139
		}
140
141
		exec($cmd, $output, $returnCode);
142
143
		if ($returnCode === 0) {
144
			return $tmpPath;
145
		}
146
147
		return false;
148
	}
149
150
	/**
151
	 * @param int $maxX
152
	 * @param int $maxY
153
	 * @param string $absPath
154
	 * @param int $second
155
	 * @return bool|\OCP\IImage
156
	 */
157
	private function generateThumbNail($maxX, $maxY, $absPath, $second) {
158
159
		$extractedCover = $this->extractMp4CoverArtwork($absPath);
160
		if (false !== $extractedCover) {
161
			$tmpPath = $extractedCover;
162
		} else {
163
			$tmpPath = $this->generateFromMovie($absPath, $second);
164
		}
165
166
		if (is_string($tmpPath) && is_file($tmpPath)) {
167
			$image = new \OC_Image();
168
			$image->loadFromFile($tmpPath);
169
			unlink($tmpPath);
170
			if ($image->valid()) {
171
				$image->scaleDownToFit($maxX, $maxY);
172
				return $image;
173
			}
174
		}
175
		return false;
176
	}
177
}
178