简体   繁体   中英

File not copying to stream when deployed in Azure, yet working through VS emulator

My program takes a MP4 file uploads it to a blob then trims it down to 10 seconds and re-uploads it to the blob. I currently have this working when running it through the azure emulator in VS however, when I deploy it to the cloud the output files have 0 bytes.

Trimmer:

public static void Trimmer(string localStoragePath, Stream output )
{
    string path2= localStoragePath.Remove(localStoragePath.Length-4 )+ "_trimmed.mp4";
    string ffmpeg = "ffmpeg.exe";
    bool success = false;
    string ExeArguments;

    try
    {

        Process proc;
        proc = new Process();
        proc.StartInfo.FileName = ffmpeg;
        ExeArguments = @" -t 10 -i " + localStoragePath + " -map_metadata 0 -acodec copy " 
                                     + path2 + " -y";
        //ExeArguments = @"–t 10 -i -acodec" + localStoragePath + path2 ;
        Trace.TraceInformation(ExeArguments);
        proc.StartInfo.Arguments = ExeArguments;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.ErrorDialog = false;
        proc.Start();
        proc.WaitForExit();
        success = true;
    }

    catch { }

    Trace.TraceInformation(string.Format("Video has been trimmed") );

    using (Stream file = File.OpenRead(path2))
    {
        Trace.TraceInformation(string.Format("Video moving to CopyStream"));
        copyStream(file, output);
    }
}

Copy Stream:

public static void copyStream(Stream input, Stream output)
{
    Trace.TraceInformation(string.Format("Video has been trimmed and now sent to be copied to stream"));
    byte[] buffer = new byte[8 * 1024];
    int len;
    while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, len);
    }
}

Blob mover:

public static void fileMover (CloudBlob mover, string filePath, Stream input, Stream output)
        {


            string localStoragePath;
            //start string from 7th letter, eg, images
            string link = filePath.Substring(7);


                LocalResource locRes;
                locRes = RoleEnvironment.GetLocalResource("WorkerRoleLocalStorage");

                //To determine the path to the local storage resource's directory

                localStoragePath = string.Format(locRes.RootPath);

            //Moving file to local storage
            try
            {
                mover.DownloadToFile(localStoragePath + link, FileMode.OpenOrCreate);
                Trace.TraceInformation("file mover has been called " + localStoragePath + link);
                //add meta data
            }
            catch(Exception e) { }

            try
            {
                Trimmer(localStoragePath + link, output);
            }
            catch(Exception e) {}
        }

Thank you.

According to your description, I have created my project to test this issue. I could make it work as expected both on my side with storage Emulator and deployed to Azure with real storage account.

在此处输入图片说明

I noticed that you have wrapped your code with try-catch , here are some suggestions for you to locate and solve this issue:

  • You could enable diagnostics for your cloud service project to log your application logs before deploying to Azure. You could leverage System.Diagnostics.Trace to log the detailed error when you capturing the exception. For more details, you could follow this tutorial .

  • For Trimmer method, you need to trouble check the path2 file is exist. I have tested that if the file is not generated, then your output blob file would be 0 in size. I modified the Trimmer method, you could refer to it.

public static void Trimmer(string localStoragePath, CloudBlockBlob outPutBlob)
{
    string path2 = localStoragePath.Remove(localStoragePath.Length - 4) + "_trimmed.mp4";
    string ffmpeg = "ffmpeg.exe";
    bool success = false;
    string ExeArguments;
    try
    {
        Process proc;
        proc = new Process();
        proc.StartInfo.FileName = ffmpeg;
        ExeArguments = @" -t 10 -i " + localStoragePath + " -map_metadata 0 -acodec copy "
                                        + path2 + " -y";
        Trace.TraceInformation(ExeArguments);
        proc.StartInfo.Arguments = ExeArguments;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.ErrorDialog = false;
        proc.Start();
        proc.WaitForExit();
    }
    catch(Exception ex)
    {
        Trace.TraceError($"Trimmer exception:{ex.Message}\r\nStackTrace:{ex.StackTrace}");
    }

    var fi = new FileInfo(path2);
    if (fi.Exists)
    {
        success = true;
        Trace.TraceInformation(string.Format("Video has been trimmed"));
        using (Stream file = File.OpenRead(path2))
        {
            Trace.TraceInformation(string.Format("Video moving to CopyStream"));
            Trace.TraceInformation(string.Format("Video has been trimmed and now sent to be copied to stream"));
            byte[] buffer = new byte[8 * 1024];
            int len;
            using (Stream outpuStream = outPutBlob.OpenWrite())
            {
                while ((len = file.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outpuStream.Write(buffer, 0, len);
                }
            }    
        }
    }
    else
    {
        Trace.TraceWarning(string.Format("Video has not been trimmed"));
    }
}

Additionally, you could follow this tutorial to view the diagnostics data or you could leverage Microsoft Azure Storage Explorer to check the WADLogsTable of your related storage account to check your application logs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM