我想将视频发送到服务器。我正在做的是将视频转换为“ ToBase64String ”并通过 JObject 发送到我的服务器。我的问题是视频的发送速度太慢,大约20至30分钟,具体取决于文件大小和与服务器速度的连接。如何提高速度,或者有更好的方法将视频发送到服务器。我正在使用Xamarin和PHP。将视频保存到服务器硬盘时,我还使用 ffmpeg 将视频转换为较小的尺寸。我认为base64string的发送速度很快,视频(My PHP)的处理速度很慢,因为当我在代码中使用break时,应用程序正在等待服务器的响应( crresponse.IsSuccessStatusCode ) 。我认为这要么是使用ffmpeg进行视频转换,要么是视频本身创建 file_put_contents
Xamarin代码(这是我转换和发送视频的地方)
byte[] crVideoData = File.ReadAllBytes(crvideo);
crvid = Convert.ToBase64String(crVideoData);
JObject crjson = new JObject
{
{ "Video", crvid }
}
HttpClient crclient = new HttpClient();
var crresponse = await crclient.PostAsync(crlink, new StringContent(crjson.ToString(), Encoding.UTF8, crcontentType));
if (crresponse.IsSuccessStatusCode)
{
await conn.QueryAsync<CAFTable>("UPDATE tblCaf SET LastSync = ? WHERE CAFNo = ?", DateTime.Parse(current_datetime), crcafNo);
}
PHP代码(这是我接收和解码base64string的地方)
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$Video = $json_obj->Video;
$ContactID = $json_obj->ContactID;
$video_decode = base64_decode($Video);
$video_filename = __DIR__ . '/uploads/'. $CAF . '_'.$CafDate.'_VID.mp4';
$video_dbfilename = './uploads/'. $CAF . '_'.$CafDate.'_VID.mp4';
$save_video = file_put_contents($video_filename, $video_decode);
if(file_exists($video_filename)) {
$temp_video_filename = __DIR__ . '/tmp/'. $CAFNo . '_'.$CafDate.'_VID.mp4';
exec('ffmpeg -i '.$video_filename.' -c:v libx264 '.$temp_video_filename.' 2>&1');
unlink($video_filename);
rename($temp_video_filename, $video_filename);
}