movie.php 2.51 KB
Newer Older
Georg Ehrke's avatar
Georg Ehrke committed
1
2
3
<?php
/**
 * Copyright (c) 2013 Frank Karlitschek frank@owncloud.org
Georg Ehrke's avatar
Georg Ehrke committed
4
 * Copyright (c) 2013 Georg Ehrke georg@ownCloud.com
Georg Ehrke's avatar
Georg Ehrke committed
5
6
7
8
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */
Georg Ehrke's avatar
Georg Ehrke committed
9
10
namespace OC\Preview;

11
		class Movie extends Provider {
12
13
			public static $avconvBinary;
			public static $ffmpegBinary;
Georg Ehrke's avatar
Georg Ehrke committed
14

15
16
17
18
19
			public function getMimeType() {
				return '/video\/.*/';
			}

			public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
20
				// TODO: use proc_open() and stream the source file ?
Georg Ehrke's avatar
Georg Ehrke committed
21

22
23
				$fileInfo = $fileview->getFileInfo($path);
				$useFileDirectly = (!$fileInfo->isEncrypted() && !$fileInfo->isMounted());
Georg Ehrke's avatar
Georg Ehrke committed
24

25
26
27
28
29
30
31
32
33
34
35
36
				if ($useFileDirectly) {
					$absPath = $fileview->getLocalFile($path);
				} else {
					$absPath = \OC_Helper::tmpFile();

					$handle = $fileview->fopen($path, 'rb');

					// we better use 5MB (1024 * 1024 * 5 = 5242880) instead of 1MB.
					// in some cases 1MB was no enough to generate thumbnail
					$firstmb = stream_get_contents($handle, 5242880);
					file_put_contents($absPath, $firstmb);
				}
37

38
39
40
41
42
43
44
45
				$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
				if ($result === false) {
					$result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
					if ($result === false) {
						$result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
					}
				}

46
47
48
				if (!$useFileDirectly) {
					unlink($absPath);
				}
49
50
51
52
53

				return $result;
			}

			/**
54
55
56
57
58
			 * @param int $maxX
			 * @param int $maxY
			 * @param string $absPath
			 * @param string $tmpPath
			 * @param int $second
59
60
61
62
63
64
			 * @return bool|\OC_Image
			 */
			private function generateThumbNail($maxX, $maxY, $absPath, $second)
			{
				$tmpPath = \OC_Helper::tmpFile();

65
				if (self::$avconvBinary) {
66
					$cmd = self::$avconvBinary . ' -an -y -ss ' . escapeshellarg($second) .
67
						' -i ' . escapeshellarg($absPath) .
68
						' -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) .
69
						' > /dev/null 2>&1';
70
				} else {
71
					$cmd = self::$ffmpegBinary . ' -y -ss ' . escapeshellarg($second) .
72
73
74
						' -i ' . escapeshellarg($absPath) .
						' -f mjpeg -vframes 1' .
						' -s ' . escapeshellarg($maxX) . 'x' . escapeshellarg($maxY) .
Vincent Petry's avatar
Vincent Petry committed
75
						' ' . escapeshellarg($tmpPath) .
76
77
						' > /dev/null 2>&1';
				}
78

79
				exec($cmd, $output, $returnCode);
Georg Ehrke's avatar
Georg Ehrke committed
80

81
				if ($returnCode === 0) {
82
83
					$image = new \OC_Image();
					$image->loadFromFile($tmpPath);
84
85
86
					unlink($tmpPath);
					return $image->valid() ? $image : false;
				}
87
				unlink($tmpPath);
88
				return false;
89
			}
Georg Ehrke's avatar
Georg Ehrke committed
90
		}