2

I want to send video to server. What I am doing is I am converting the video into a "ToBase64String" and sending to my server via JObject. My problem is the sending of the video is too slow about 20 to 30 mins depending on the file size and connection to server speed. How can I improve the speed or is there a better way to send a video to a server. I am using Xamarin and PHP. I am also using ffmpeg to convert the video into a smaller size when saving the video to the server's hard drive. I think the sending of the base64string is fast the processing of the video(My PHP) is slow because when I use break in my code the app is waiting for the response from my server(crresponse.IsSuccessStatusCode). I think it is either the conversion of the video using ffmpeg or the creation of the video itself file_put_contents

Xamarin Code (this is where I convert and send the video)

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 code (this is where I receive and decode the 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);
}
  • 2
    Don't make wild guesses about performance problems. Get hard data about where the bottleneck is by timing your code and fix it. And you should not need to encode a video stream to upload it - you can just send a byte[]. – Jason Dec 18 '18 at 13:08
  • @Jason how can I send it via JSON object –  Dec 18 '18 at 13:11
  • 2
    I would suggest you send a byte[] data rather than using base64 – FreakyAli Dec 18 '18 at 13:51
  • @G.hakim can you remote my computer? there is an error when I tried to send a byte via JObject? –  Dec 18 '18 at 13:55
  • Sure we can do that tomorrow – FreakyAli Dec 18 '18 at 20:30
  • @G.hakim I solved the error I can now send the byte not the base64string now the problem really is the speed of transmission any suggestions? –  Dec 19 '18 at 00:12
  • Well, i would suggest you see to it that it is async, doesn't have unnecessary declarations and usage of objects that are not necessary then i would suggest you do the same at API side and see how the performance gets – FreakyAli Dec 19 '18 at 06:31
  • I think the API is the problem i benched marked the speed when uploading a less than 1mb video it takes about 1-2 minutes for the file to be created the byte array has been sent almost instantly the file creation takes too long –  Dec 19 '18 at 06:39
  • @G.hakim bro the file_put_contents is taking to long how can I improve this? –  Dec 19 '18 at 16:22
  • Can you add the code that you are using to create the video? – FreakyAli Dec 20 '18 at 05:50
  • @G.hakim the code is the PHP part –  Dec 20 '18 at 14:29

0 Answers0