movie.php 2.41 KB
Newer Older
Georg Ehrke's avatar
Georg Ehrke committed
1
2
<?php
/**
3
4
5
6
7
 * Copyright (c) 2013 Frank Karlitschek frank@owncloud.org
 * Copyright (c) 2013 Georg Ehrke georg@ownCloud.com
 * 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
8
 */
Georg Ehrke's avatar
Georg Ehrke committed
9
10
namespace OC\Preview;

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

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

22
23
24
25
26
	/**
	 * {@inheritDoc}
	 */
	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
		// TODO: use proc_open() and stream the source file ?
Georg Ehrke's avatar
Georg Ehrke committed
27

28
29
		$fileInfo = $fileview->getFileInfo($path);
		$useFileDirectly = (!$fileInfo->isEncrypted() && !$fileInfo->isMounted());
Georg Ehrke's avatar
Georg Ehrke committed
30

31
32
33
34
		if ($useFileDirectly) {
			$absPath = $fileview->getLocalFile($path);
		} else {
			$absPath = \OC_Helper::tmpFile();
35

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

38
39
40
41
42
			// 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);
		}
43

44
45
46
47
48
49
50
		$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);
			}
		}
51

52
53
54
		if (!$useFileDirectly) {
			unlink($absPath);
		}
55

56
57
		return $result;
	}
58

59
60
61
62
63
	/**
	 * @param int $maxX
	 * @param int $maxY
	 * @param string $absPath
	 * @param int $second
64
	 * @return bool|\OCP\IImage
65
66
67
	 */
	private function generateThumbNail($maxX, $maxY, $absPath, $second) {
		$tmpPath = \OC_Helper::tmpFile();
68

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

83
		exec($cmd, $output, $returnCode);
Georg Ehrke's avatar
Georg Ehrke committed
84

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