-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_processor.php
More file actions
133 lines (107 loc) · 5.17 KB
/
background_processor.php
File metadata and controls
133 lines (107 loc) · 5.17 KB
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
129
130
131
132
133
<?php
// background_processor.php - FIXED VERSION
set_time_limit(0);
ini_set('max_execution_time', 0);
// Mulai session untuk akses session variables
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// DEBUG: Log semua available data
file_put_contents('debug_background.log',
"=== BACKGROUND PROCESSOR STARTED ===\n" .
"Time: " . date('Y-m-d H:i:s') . "\n" .
"argv: " . print_r($argv, true) . "\n" .
"SESSION: " . print_r($_SESSION, true) . "\n",
FILE_APPEND);
// Method 1: Baca dari file (PRIORITY)
$filename = '';
$filenameFile = 'current_filename.txt';
if (file_exists($filenameFile)) {
$filename = trim(file_get_contents($filenameFile));
file_put_contents('debug_background.log', "Read from file: $filename\n", FILE_APPEND);
// Hapus file setelah dibaca
unlink($filenameFile);
}
// Method 2: Dari session
if (empty($filename) && isset($_SESSION['current_video']) && !empty($_SESSION['current_video'])) {
$filename = $_SESSION['current_video'];
file_put_contents('debug_background.log', "Read from session: $filename\n", FILE_APPEND);
}
// Method 3: Dari command line arguments
if (empty($filename) && isset($argv[1]) && !empty($argv[1])) {
$filename = $argv[1];
file_put_contents('debug_background.log', "Read from argv: $filename\n", FILE_APPEND);
}
// ERROR jika tidak ada filename
if (empty($filename)) {
$errorMsg = "❌ ERROR: Tidak ada filename yang provided\n";
file_put_contents('process.log', $errorMsg, FILE_APPEND);
file_put_contents('process_error', $errorMsg);
file_put_contents('debug_background.log', $errorMsg, FILE_APPEND);
exit(1);
}
file_put_contents('debug_background.log', "Final filename: $filename\n", FILE_APPEND);
function runProcess($filename) {
$ffmpegPath = '"C:\\ProgramData\\chocolatey\\bin\\ffmpeg.exe"';
$pythonPath = '"C:\\Program Files\\PhpWebStudy-Data\\app\\python-3.13.3\\python.exe"';
$baseName = pathinfo($filename, PATHINFO_FILENAME);
$inputFile = 'upload/' . $filename;
$outputFile = 'audio/' . $baseName . '.wav';
$subtitleFile = 'subtitles/' . $baseName . '.ass';
$outputVideo = 'output/' . $baseName . '_with_subtitle.mp4';
$logFile = 'process.log';
file_put_contents($logFile, "\n📁 FILE PATHS:\n", FILE_APPEND);
file_put_contents($logFile, "Input: $inputFile\n", FILE_APPEND);
file_put_contents($logFile, "Audio: $outputFile\n", FILE_APPEND);
file_put_contents($logFile, "Subtitle: $subtitleFile\n", FILE_APPEND);
file_put_contents($logFile, "Output: $outputVideo\n\n", FILE_APPEND);
// Buat directories
if (!is_dir('audio')) mkdir('audio', 0777, true);
if (!is_dir('output')) mkdir('output', 0777, true);
if (!is_dir('subtitles')) mkdir('subtitles', 0777, true);
try {
// STEP 1: Ekstrak Audio dari file uploaded
file_put_contents($logFile, "🎧 STEP 1: Ekstrak audio dari $filename...\n", FILE_APPEND);
if (!file_exists($inputFile)) {
throw new Exception("File input tidak ditemukan: $inputFile");
}
$ffmpegCmd = "$ffmpegPath -y -i $inputFile -vn -ar 16000 -ac 1 -f wav $outputFile 2>&1";
file_put_contents($logFile, "Command: $ffmpegCmd\n", FILE_APPEND);
$output = shell_exec($ffmpegCmd);
file_put_contents($logFile, $output . "\n", FILE_APPEND);
if (!file_exists($outputFile)) {
throw new Exception("Gagal membuat file audio: $outputFile");
}
file_put_contents($logFile, "✅ Audio berhasil: " . filesize($outputFile) . " bytes\n\n", FILE_APPEND);
// STEP 2: Transkripsi
file_put_contents($logFile, "🎤 STEP 2: Transkripsi audio...\n", FILE_APPEND);
$pythonCmd = "$pythonPath transcribe.py \"$filename\" 2>&1";
file_put_contents($logFile, "Command: $pythonCmd\n", FILE_APPEND);
$pythonOutput = shell_exec($pythonCmd);
file_put_contents($logFile, $pythonOutput . "\n", FILE_APPEND);
if (!file_exists($subtitleFile)) {
throw new Exception("File subtitle tidak dibuat: $subtitleFile");
}
file_put_contents($logFile, "✅ Subtitle berhasil: $subtitleFile\n\n", FILE_APPEND);
// STEP 3: Merge Video
file_put_contents($logFile, "🎬 STEP 3: Merge video dengan subtitle...\n", FILE_APPEND);
$mergeCmd = "$ffmpegPath -y -i $inputFile -vf \"ass=$subtitleFile\" $outputVideo 2>&1";
file_put_contents($logFile, "Command: $mergeCmd\n", FILE_APPEND);
$mergeOutput = shell_exec($mergeCmd);
file_put_contents($logFile, $mergeOutput . "\n", FILE_APPEND);
if (!file_exists($outputVideo)) {
throw new Exception("File video output tidak dibuat: $outputVideo");
}
file_put_contents($logFile, "✅ Video berhasil: $outputVideo - " . filesize($outputVideo) . " bytes\n\n", FILE_APPEND);
file_put_contents('process_done', 'completed');
file_put_contents($logFile, "🎉 PROSES SELESAI!\n", FILE_APPEND);
return true;
} catch (Exception $e) {
file_put_contents($logFile, "❌ ERROR: " . $e->getMessage() . "\n", FILE_APPEND);
file_put_contents('process_error', $e->getMessage());
return false;
}
}
// Jalankan proses
runProcess($filename);
?>