-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFfmpegVideoFile.php
122 lines (102 loc) · 4.41 KB
/
FfmpegVideoFile.php
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
<?php
require_once(dirname(__FILE__) . "/FfmpegVideoCodecs.php");
require_once(dirname(__FILE__) . "/FfmpegVideoTranscoding.php");
Class FfmpegVideoFile {
private $filename;
private $ffmpeg;
private $video;
private $movie;
private $rotation;
function __construct($name, $options = array()) {
$this->filename = $name;
$ffmpeg_binary = "ffmpeg";
if (FfmpegVideoTranscoding::$ffmpeg_binary)
$ffmpeg_binary = FfmpegVideoTranscoding::$ffmpeg_binary;
$this->movie = new FFmpegMovie($name, new FFmpegCustomOutputProvider($ffmpeg_binary), $ffmpeg_binary);
$config = array();
if (FfmpegVideoTranscoding::$ffmpeg_binary)
$config["ffmpeg.binaries"] = array(FfmpegVideoTranscoding::$ffmpeg_binary);
if (FfmpegVideoTranscoding::$ffprobe_binary)
$config["ffprobe.binaries"] = array(FfmpegVideoTranscoding::$ffprobe_binary);
$this->ffmpeg = FFMpeg\FFMpeg::create($config);
$this->video = $this->ffmpeg->open($name);
$this->rotation = @$options["rotate_add"] ? $options["rotate_add"] : 0;
$this->rotation_write = $this->rotation;
$this->autorotate = @$options["autorotate"] ? $options["autorotate"] : FALSE;
if (@$options["rotation"]) {
try {
$this->rotation += FfmpegVideoTranscoding::getRotation($name);
if (!$this->autorotate)
$this->rotation_write = $this->rotation;
} catch (Exception $e) {
}
}
}
function getRotation() {
return $this->rotation;
}
function hasVideo() {
return $this->movie->hasVideo();
}
function hasAudio() {
return $this->movie->hasAudio();
}
function getDuration() {
return $this->movie->getDuration();
}
function getWidth() {
return $this->rotation % 180 == 0 ? $this->movie->getFrameWidth() : $this->movie->getFrameHeight();
}
function getHeight() {
return $this->rotation % 180 == 0 ? $this->movie->getFrameHeight() : $this->movie->getFrameWidth();
}
function getVideoCodec() {
return $this->movie->getVideoCodec();
}
function getAudioCodec() {
return $this->movie->getAudioCodec();
}
private function getTempFileName() {
return tempnam(sys_get_temp_dir(), "");
}
function saveImageBySecond($filename = NULL, $seconds = 0, $extension = "png", $safeRevertToZero = FALSE) {
$filename = $filename == NULL ? $this->getTempFileName() . "." . $extension : $filename;
touch($filename);
$frameCount = $this->movie->getFrameCount();
if ($frameCount > 0) {
$frameDuration = $this->getDuration() / $frameCount;
if (ceil($seconds / $frameDuration) >= $frameCount)
$seconds = ($frameCount - 1) * $frameDuration;
}
$frame = $this->video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($seconds));
if (@$this->rotation_write)
$frame->addFilter(new RotationFrameFilter($this->rotation_write));
$frame->save($filename);
if ($safeRevertToZero && !is_file($filename)) {
$frame = $this->video->frame(0);
if (@$this->rotation_write)
$frame->addFilter(new RotationFrameFilter($this->rotation_write));
$frame->save($filename);
}
return $filename;
}
function saveImageByPercentage($filename = NULL, $percentage = 0, $extension = "png", $safeRevertToZero = FALSE) {
return $this->saveImageBySecond($filename, $percentage * $this->getDuration(), $extension, $safeRevertToZero);
}
function saveAudio($filename = NULL, $extension = "wav") {
$filename = $filename == NULL ? $this->getTempFileName() . "." . $extension : $filename;
touch($filename);
$audio = new FFMpeg\Media\Audio($this->filename, $this->ffmpeg->getFFMpegDriver(), $this->ffmpeg->getFFProbe());
$audio->save(new NullAudio(), $filename);
return $filename;
}
function getVideoType($filename = NULL) {
return FfmpegVideoCodecs::videoTypeByCodecAndFileName($this->getVideoCodec(), @$filename ? $filename : $this->filename);
}
function getVideoSubType() {
return FfmpegVideoCodecs::videoSubTypeByCodec($this->getVideoCodec());
}
function getAudioSubType() {
return FfmpegVideoCodecs::audioSubTypeByCodec($this->getAudioCodec());
}
}