0

I have a website hosted by Azure that includes a Web API which I'm using to develop an android app. I'm trying to upload a media file to the server where it's encoded by a media encoder and saved to a path. The encoder library is called "Media Toolkit" which I found here : https://www.nuget.org/packages/MediaToolkit/1.0.0.3

My server side code looks like this:

        [HttpPost]
    [Route("upload")]
    public async Task<HttpResponseMessage> Upload(uploadFileModel model)
    {


        var result = new HttpResponseMessage(HttpStatusCode.OK);

        if (ModelState.IsValid)
        {
            string thumbname = "";
            string resizedthumbname = Guid.NewGuid() + "_yt.jpg";

            string FfmpegPath = Encoding_Settings.FFMPEGPATH;

            string tempFilePath = Path.Combine(HttpContext.Current.Server.MapPath("~/video"), model.fileName);
            string pathToFiles = HttpContext.Current.Server.MapPath("~/video");
            string pathToThumbs = HttpContext.Current.Server.MapPath("~/contents/member/" + model.username + "/thumbs");
            string finalPath = HttpContext.Current.Server.MapPath("~/contents/member/" + model.username + "/flv");
            string resizedthumb = Path.Combine(pathToThumbs, resizedthumbname);


            var outputPathVid = new MediaFile { Filename = Path.Combine(finalPath, model.fileName) };
            var inputPathVid = new MediaFile { Filename = Path.Combine(pathToFiles, model.fileName) };
            int maxWidth = 380;
            int maxHeight = 360;

            var namewithoutext = Path.GetFileNameWithoutExtension(Path.Combine(pathToFiles, model.fileName));
            thumbname = model.VideoThumbName;
            string oldthumbpath = Path.Combine(pathToThumbs, thumbname);


            var fileName = model.fileName;
            try
            {
                File.WriteAllBytes(tempFilePath, model.data);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            using (var engine = new Engine())
            {
                engine.GetMetadata(inputPathVid);

                // Saves the frame located on the 15th second of the video.
                var outputPathThumb = new MediaFile { Filename = Path.Combine(pathToThumbs, thumbname + ".jpg") };
                var options = new ConversionOptions { Seek = TimeSpan.FromSeconds(0), CustomHeight = 360, CustomWidth = 380 };
                engine.GetThumbnail(inputPathVid, outputPathThumb, options);

            }

            Image image = Image.FromFile(Path.Combine(pathToThumbs, thumbname + ".jpg"));

            //var ratioX = (double)maxWidth / image.Width;
            //var ratioY = (double)maxHeight / image.Height;
            //var ratio = Math.Min(ratioX, ratioY);

            var newWidth = (int)(maxWidth);
            var newHeight = (int)(maxHeight);

            var newImage = new Bitmap(newWidth, newHeight);
            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
            Bitmap bmp = new Bitmap(newImage);
            bmp.Save(Path.Combine(pathToThumbs, thumbname + "_resized.jpg"));

            //File.Delete(Path.Combine(pathToThumbs, thumbname));

            using (var engine = new Engine())
            {

                var conversionOptions = new ConversionOptions
                {
                    VideoSize = VideoSize.Hd720,
                    AudioSampleRate = AudioSampleRate.Hz44100,
                    VideoAspectRatio = VideoAspectRatio.Default
                };

                engine.GetMetadata(inputPathVid);
                engine.Convert(inputPathVid, outputPathVid, conversionOptions);

            }

            File.Delete(tempFilePath);

            Video_Struct vd = new Video_Struct();
            vd.CategoryID = 0; // store categoryname or term instead of category id
            vd.Categories = "";
            vd.UserName = model.username;
            vd.Title = "";
            vd.Description = "";
            vd.Tags = "";
            vd.Duration = inputPathVid.Metadata.Duration.ToString();
            vd.Duration_Sec = Convert.ToInt32(inputPathVid.Metadata.Duration.Seconds.ToString());
            vd.OriginalVideoFileName = model.fileName;
            vd.VideoFileName = model.fileName;
            vd.ThumbFileName = thumbname + "_resized.jpg";
            vd.isPrivate = 0;
            vd.AuthKey = "";
            vd.isEnabled = 1;
            vd.Response_VideoID = 0; // video responses
            vd.isResponse = 0;
            vd.isPublished = 1;
            vd.isReviewed = 1;
            vd.Thumb_Url = "none";
            //vd.FLV_Url = flv_url;
            vd.Embed_Script = "";
            vd.isExternal = 0; // website own video, 1: embed video
            vd.Type = 0;
            vd.YoutubeID = "";
            vd.isTagsreViewed = 1;
            vd.Mode = 0; // filter videos based on website sections
            //vd.ContentLength = f_contentlength;
            vd.GalleryID = 0;
            long videoid = VideoBLL.Process_Info(vd, false);


            return result;
        }

        else
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
        }

    }

When the debugger hits the line using (var engine = new Engine()) I get 500 internal server error thrown. I don't get this error testing it on the iis server. Since it works fine on my local server and not on the azure hosted server, I figured it had to do with the Azure service rather than an error in my code. If so is the case then how would I be able to get around this issue? I don't want to use azure blob storage as it would require a lot of changes to my code. Does anyone have any idea what might be the issue. Any helpful suggestions are appreciated.

Ahmed Mujtaba
  • 2,110
  • 5
  • 36
  • 67

1 Answers1

0

Server.MapPath works differently on Azure WebApps - change to: string pathToFiles = HttpContext.Current.Server.MapPath("~//video");

Also, see this SO post for another approach.

Community
  • 1
  • 1
viperguynaz
  • 12,044
  • 4
  • 30
  • 41