Esempio n. 1
0
        private void StartEncode(MetadataServiceConfigurator serviceConfigurator, string[] filePathes, string prefix = "")
        {
            int    count         = 0;
            int    excelStrCount = 2;
            double avgEncodeFps  = 0;
            var    errorVideos   = new List <string>();
            var    allSw         = new Stopwatch();
            var    decodeSw      = new Stopwatch();
            Excel  excel         = CreateExcel();

            foreach (string filePath in filePathes)
            {
                allSw.Restart();
                Trace.WriteLine(String.Format("\n-------------Start decode file: {0}-------------\n", filePath));

                try
                {
                    string         destinationFileName = GetDestinationFileName(filePath, prefix);
                    var            mediaInfo           = new MediaInfoWrapper();
                    var            metadataInfo        = new VideoMetadataInfo(mediaInfo);
                    VideoMediaInfo metadata            = metadataInfo.GetMetadata(filePath);
                    var            metadataService     = new MetadataService(serviceConfigurator, metadata);
                    var            stringBuilder       = new FfmpegService(metadataService, filePath, _destinationPath, destinationFileName);
                    var            ffmpeg       = new Ffmpeg(stringBuilder);
                    string         ffmpegString = stringBuilder.GetStringForEncoder();

                    WriteFileInfo(ffmpegString, metadata);

                    decodeSw.Restart();
                    ffmpeg.StartEncodeProcess();
                    allSw.Stop();
                    decodeSw.Stop();
                    WriteFinishProcess(decodeSw.Elapsed, allSw.Elapsed, ffmpeg.EncodeFps);

                    if (ffmpeg.EncodeFps > 0)
                    {
                        SetExcelLine(excel, metadata, filePath, excelStrCount, ffmpeg.EncodeFps);
                        excelStrCount++;
                    }

                    avgEncodeFps += ffmpeg.EncodeFps;
                    count++;
                }
                catch (MediaFormatException ex)
                {
                    string errorMessage = String.Format("Error File:\t\t{0}.\nError Param:\t\t{1}", filePath, ex.Message);
                    Trace.WriteLine(errorMessage);
                    errorVideos.Add(errorMessage);
                }
                catch (ExternalProcessException ex)
                {
                    string errorMessage = String.Format("Error File:\t\t{0}.\nFfmpeg return:\t\t{1}\n{2}", filePath, ex.Result, ex.Arguments);
                    Trace.WriteLine(errorMessage);
                    errorVideos.Add(errorMessage);
                }
                finally
                {
                    Trace.WriteLine(String.Format("\n-------------Finish decode file: {0}-------------\n", filePath));
                }
            }

            excel.SaveDocument(String.Format(@"{0}\{1}_{2}.xlsx", _destinationPath, serviceConfigurator.Container, DateTime.Now.ToString("u").Replace(":", "-")));
            excel.CloseDocument();

            avgEncodeFps = avgEncodeFps / count;
            WriteFinishInfo(avgEncodeFps, count, errorVideos);
        }