-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-video.php
executable file
·128 lines (112 loc) · 5 KB
/
make-video.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
126
127
128
<?php
include 'videoConfig.php';
$videoGenerator = new videoGenerator();
$videoGenerator->setVideoFolder($GLOBALS['config']['videoFolder']);
foreach ($videoGenerator->getOriginalVideos() as $originalVideo) {
echo "Processing video " . $originalVideo . PHP_EOL;
$videoGenerator->setOriginalVideoPath($originalVideo);
$videoGenerator->setOverlayVideoPath($GLOBALS['config']['videoFolder'] . '/output/' . pathinfo($originalVideo)['filename'] . '.overlay.mp4');
$dataLines = $videoGenerator->getDataForVideo();
#foreach ($dataLines as $dataLine) {
#list($frameNumber, $text) = explode(',', $dataLine);
#$videoGenerator->generateImage($frameNumber, $text);
#}
if (!file_exists($videoGenerator->getOverlayVideoPath())) {
$videoGenerator->createOverlayVideo();
} else {
echo "Datei " . $videoGenerator->getOverlayVideoPath() . " existiert bereits und wird übersprungen.";
}
#$videoGenerator->deleteImages();
}
class videoGenerator
{
protected $videoFolder = '';
protected $imagesFolder = 'video/2024-05-13/images';
protected $originalVideoPath = '';
protected $overlayVideoPath = '';
protected $frameDataPath = '';
public function getOriginalVideos()
{
$files = scandir($this->videoFolder);
$fileExtensions = ['mp4'];
foreach ($files as $filekey => $file) {
foreach ($fileExtensions as $fileExtension) {
if (substr($file, -strlen($fileExtension)) !== $fileExtension) {
unset($files[$filekey]);
} elseif (str_replace('overlay', '', $file) !== $file) {
unset($files[$filekey]);
} else {
$files[$filekey] = $this->videoFolder . '/' . $file;
}
}
}
#var_dump($files);
return $files;
}
public function getDataForVideo()
{
$filename = pathinfo($this->originalVideoPath, PATHINFO_FILENAME);
$dirname = pathinfo($this->originalVideoPath, PATHINFO_DIRNAME);
$this->frameDataPath = $dirname . '/' . $filename . '.frameData.csv';
echo "Frame Data Path is " . $this->frameDataPath . PHP_EOL;
// Read the text data file
$lines = file($this->frameDataPath, FILE_IGNORE_NEW_LINES);
return $lines;
}
public function generateImage(int $frameNumber, string $text)
{
$command = "convert -size 1920x1080 xc:none -gravity south -pointsize 24 -fill white -annotate +0+5 \"$text\" $this->imagesFolder/frame_" . sprintf('%04d', $frameNumber) . ".png";
echo $command . PHP_EOL;
exec($command);
}
public function createOverlayVideo()
{
#$ffmpegCommand = "ffmpeg -i $this->originalVideoPath -pattern_type glob -framerate 30 -i \"$this->imagesFolder/*.png\" -filter_complex overlay $this->overlayVideoPath";
#$ffmpegCommand = "ffmpeg -i $this->originalVideoPath -vf \"sendcmd=f=$this->frameDataPath\" -c:a copy $this->overlayVideoPath";
#$ffmpegCommand = "ffmpeg -i $this->originalVideoPath -c:v libx264 -crf 18 -vf \"sendcmd=f=$this->frameDataPath,drawtext=x=0:y=H-th-30:fontcolor=white:fontsize=28:fontfile=/usr/share/fonts/truetype/roboto/hinted/Roboto-Regular.ttf:box=1:boxcolor=black:expansion=none:text=''\" -c:a copy $this->overlayVideoPath";
#$ffmpegCommand = "ffmpeg -i $this->originalVideoPath -c:v h264_nvenc -cq 18 -vf \"sendcmd=f=$this->frameDataPath,drawtext=x=0:y=H-th-30:fontcolor=white:fontsize=28:fontfile=/usr/share/fonts/truetype/roboto/hinted/Roboto-Regular.ttf:box=1:boxcolor=black:expansion=none:text=''\" -c:a copy $this->overlayVideoPath";
$ffmpegCommand = "ffmpeg -i $this->originalVideoPath -c:v h264_nvenc -cq 18 -vf \"scale=2560:1440,sendcmd=f=$this->frameDataPath,drawtext=x=0:y=H-th-40:fontcolor=white:fontsize=36:fontfile=/usr/share/fonts/truetype/roboto/hinted/Roboto-Regular.ttf:box=1:boxcolor=black@0.9:expansion=none:text=''\" -c:a copy $this->overlayVideoPath";
echo $ffmpegCommand . PHP_EOL;
exec($ffmpegCommand);
}
public function deleteImages()
{
}
/**
* @param string $originalVideoPath
*/
public function setOriginalVideoPath(string $originalVideoPath): void
{
$this->originalVideoPath = $originalVideoPath;
}
public function getOriginalVideoPath(): string
{
return $this->originalVideoPath;
}
/**
* @param string $overlayVideoPath
*/
public function setOverlayVideoPath(string $overlayVideoPath): void
{
echo "setting video output to " . $overlayVideoPath . PHP_EOL;
$this->overlayVideoPath = $overlayVideoPath;
}
public function getOverlayVideoPath(): string
{
return $this->overlayVideoPath;
}
/**
* @return string
*/
public function getVideoFolder(): string
{
return $this->videoFolder;
}
/**
* @param string $videoFolder
*/
public function setVideoFolder(string $videoFolder): void
{
$this->videoFolder = $videoFolder;
}
}