<?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' => '文件上传错误'));
}
