food_server/upload.php
2024-05-30 19:16:59 +08:00

32 lines
1.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
header('Content-Type: application/json');
unlink("uploads/myaudiofile.mp3");
unlink("uploads/ffmpeg_myaudiofile.mp3");
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = 'uploads/';
$targetFile = $targetPath . basename($_FILES['file']['name']);
if (move_uploaded_file($tempFile, $targetFile)) {
// 输入文件路径
$inputFilePath = 'uploads/myaudiofile.mp3';
// 输出文件路径
$outputFilePath = 'uploads/ffmpeg_myaudiofile.mp3';
// FFmpeg命令使用完整路径注意在Windows下要使用双引号
$ffmpegCommand = 'C:\ffmpeg\bin\ffmpeg.exe -i ' . $inputFilePath . ' -b:a 128k ' . $outputFilePath . ' 2>&1';
// 执行FFmpeg命令并获取标准错误输出
exec($ffmpegCommand, $output, $resultCode);
// 检查转码是否成功
if ($resultCode === 0) {
echo json_encode(array('status' => 1, 'msg' => '音频转码成功'));
} else {
echo json_encode(array('status' => 0, 'msg' => "错误信息:" . implode(PHP_EOL, $output)));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '移动上传文件错误'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '文件上传错误'));
}