-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscoding.php
125 lines (103 loc) · 4.57 KB
/
transcoding.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
123
124
125
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
include 'util.php';
class Transcoding {
/**
* Class constructor
*/
function __construct($tmp_name, $duration, $name) {
$this->util = new Util();
$this->config = include 'config.php';
$this->HOST = $this->config['host'];
$this->HOST_PATH = $this->config['host_path'];
$this->UPLOAD_DIR = $this->config['upload_dir'];
$this->TMP_DIR = $this->config['tmp_dir'];
$this->STILLS_DIR = $this->config['stills_dir'];
$this->convertVideos($tmp_name, $name);
$this->extractImages($tmp_name, $duration, 4, $name);
}
/**
* Send server site event messages and progress information to the client.
*/
function send_message($id, $message, $progress) {
$d = array('message' => $message , 'progress' => $progress);
echo "id: $id" . PHP_EOL;
echo "data: " . json_encode($d) . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
}
/**
* Converts a given video file into an webm and mp4 file
*/
function convertVideos($filename, $name){
// initialize
require_once 'vendor/autoload.php';
$ffmpeg = FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' => $this->config['ffmpeg'],
'ffprobe.binaries' => $this->config['ffprobe'],
'timeout' => 360000, // The timeout for the underlying process
'ffmpeg.threads' => 16, // The number of threads that FFMpeg should use
));
$ffmpeg = FFMpeg\FFMpeg::create();
// open video
$video = $ffmpeg->open( $filename );
$formatx264 = new FFMpeg\Format\Video\X264();
$formatx264->setAudioCodec("libmp3lame"); // libvorbis libmp3lame libfaac
$formatx264->on('progress', function ($audio, $format, $percentage) {
$percentage = is_numeric($percentage) ? $percentage : 0;
$this->send_message($percentage, 'x264' , $percentage);
});
$formatwebm = new FFMpeg\Format\Video\WebM();
$formatwebm->setAudioCodec("libvorbis");
$formatwebm->on('progress', function ($audio, $format, $percentage) {
$percentage = is_numeric($percentage) ? $percentage : 0;
$this->send_message($percentage, 'x264' , $percentage);
});
$video->save($formatx264, $this->TMP_DIR . '/' . $name . '.mp4');
$video->save($formatwebm, $this->TMP_DIR . '/' . $name . '.webm');
}
/**
* Extracts a given number of still images from a video
*/
function extractImages( $videofile, $duration, $n, $name ){
require_once 'vendor/autoload.php';
$ffmpeg = FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' => $this->config['ffmpeg'],
'ffprobe.binaries' => $this->config['ffprobe'],
'timeout' => 360000, // The timeout for the underlying process
'ffmpeg.threads' => 16 // The number of threads that FFMpeg should use
));
$video = $ffmpeg->open( $videofile );
if(is_dir($this->TMP_DIR) && is_writable($this->TMP_DIR)){
// generate gif animation
$video
->gif(FFMpeg\Coordinate\TimeCode::fromSeconds(0), new FFMpeg\Coordinate\Dimension(320, 240), 10)
->save( $this->TMP_DIR . '/still-' . $name . '_comp.gif');
$this->send_message('img-ani', 'animation', 100);
// generate a thumbnail image
$video
->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(round($duration/2)))
->save( $this->TMP_DIR . '/still-' . $name . '_comp.jpg');
$this->send_message('img-thumb', 'thumbnail', 100);
// generate preview images per minute
for($i=0; $i < $duration; $i++){
$video
->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($i))
->save( $this->TMP_DIR . '/preview-' . $name . '-' . $i . '.jpg' );
$this->send_message('img', 'preview', round( ($i / $duration) * 100));
}
$this->send_message('img', 'preview', 100);
}else{
return $this->TMP_DIR . ' does not exist or is not writable';
}
}
}
// initialize class
if(isset($_GET['location']) && isset($_GET['duration']) && isset($_GET['name'])){
$obj = new Transcoding($_GET['location'], $_GET['duration'], $_GET['name']);
}else{
echo "GET: parameters missing";
}
?>