Ejemplo n.º 1
0
        public static bool Encode(FileItem fileItem)
        {
            FileItem sourceFile = fileItem.FileContainer.SourceFileItem;

            try
            {
                fileItem.SpriteEncodeProcess.StartProcessDateTime();

                LogManager.AddSpriteMessage("SourceFilePath " + Path.GetFileName(fileItem.SourceFilePath), "Start Sprite");

                // Récupérer la durée totale de la vidéo et sa résolution, autorisation sprite creation
                if (!VideoSourceManager.SuccessAnalyseSource(fileItem, true, fileItem.SpriteEncodeProcess))
                {
                    return(false);
                }

                int nbImages     = VideoSettings.NbSpriteImages;
                int heightSprite = VideoSettings.HeightSpriteImages;

                // Calculer nb image/s
                //  si < 100s de vidéo -> 1 image/s
                //  sinon (nb secondes de la vidéo / 100) image/s
                string frameRate = "1";
                int    duration  = sourceFile.VideoDuration.Value;
                if (duration > nbImages)
                {
                    frameRate = $"{nbImages}/{duration}"; //frameRate = inverse de image/s
                }

                int    spriteWidth  = SizeHelper.GetWidth(sourceFile.VideoWidth.Value, sourceFile.VideoHeight.Value, heightSprite);
                string sizeImageMax = $"scale={spriteWidth}:{heightSprite}";

                // Extract frameRate image/s de la video
                string arguments            = $"-y -i {Path.GetFileName(fileItem.SourceFilePath)} -r {frameRate} -vf {sizeImageMax} -f image2 {GetPattern(fileItem.TempFilePath)}";
                var    ffmpegProcessManager = new FfmpegProcessManager(fileItem, fileItem.SpriteEncodeProcess);
                ffmpegProcessManager.StartProcess(arguments, VideoSettings.EncodeGetImagesTimeout);
                string[] files = GetListImageFrom(fileItem.TempFilePath); // récupération des images

                LogManager.AddSpriteMessage((files.Length - 1) + " images", "Start Combine images");
                bool successSprite = CombineBitmap(files.Skip(files.Length - VideoSettings.NbSpriteImages).ToArray(), fileItem.TempFilePath); // création du sprite
                TempFileManager.SafeDeleteTempFiles(files);                                                                                   // suppression des images
                if (successSprite)
                {
                    fileItem.SetOutputFilePath(fileItem.TempFilePath);
                    LogManager.AddSpriteMessage("OutputFileName " + Path.GetFileName(fileItem.OutputFilePath) + " / FileSize " + fileItem.FileSize, "End Sprite");
                }
                else
                {
                    LogManager.AddSpriteMessage("Error while combine images", "Error");
                    fileItem.SpriteEncodeProcess.SetErrorMessage("Error creation sprite while combine images");
                    return(false);
                }

                fileItem.SpriteEncodeProcess.EndProcessDateTime();
                return(true);
            }
            catch (Exception ex)
            {
                LogManager.AddSpriteMessage("Video Duration " + sourceFile.VideoDuration + " / FileSize " + fileItem.FileSize + " / Progress " + fileItem.SpriteEncodeProcess.Progress + " / Exception : " + ex, "Exception");
                fileItem.SpriteEncodeProcess.SetErrorMessage("Exception");
                string[] files = GetListImageFrom(fileItem.TempFilePath); // récupération des images
                TempFileManager.SafeDeleteTempFiles(files);               // suppression des images
                return(false);
            }
        }
Ejemplo n.º 2
0
        public static bool Encode(FileItem fileItem)
        {
            string newEncodedFilePath = null;

            try
            {
                fileItem.EncodeProgress = "0.00%";

                FileItem  sourceFile     = fileItem.FileContainer.SourceFileItem;
                string    sourceFilePath = sourceFile.FilePath;
                VideoSize videoSize      = fileItem.VideoSize;
                LogManager.AddEncodingMessage("SourceFilePath " + Path.GetFileName(sourceFilePath) + " -> " + videoSize, "Start");

                // Récupérer la durée totale de la vidéo et sa résolution, autorisation encoding
                if (!VideoSourceManager.CheckAndAnalyseSource(fileItem, false))
                {
                    return(false);
                }

                string size;
                string maxRate = string.Empty;
                switch (videoSize)
                {
                case VideoSize.F360p:
                {
                    maxRate = "200k";
                    Tuple <int, int> finalSize = SizeHelper.GetSize(sourceFile.VideoWidth.Value, sourceFile.VideoHeight.Value, 640, 360);
                    size = $"{finalSize.Item1}:{finalSize.Item2}";
                    break;
                }

                case VideoSize.F480p:
                {
                    maxRate = "500k";
                    Tuple <int, int> finalSize = SizeHelper.GetSize(sourceFile.VideoWidth.Value, sourceFile.VideoHeight.Value, 854, 480);
                    size = $"{finalSize.Item1}:{finalSize.Item2}";
                    break;
                }

                case VideoSize.F720p:
                {
                    maxRate = "1000k";
                    Tuple <int, int> finalSize = SizeHelper.GetSize(sourceFile.VideoWidth.Value, sourceFile.VideoHeight.Value, 1280, 720);
                    size = $"{finalSize.Item1}:{finalSize.Item2}";
                    break;
                }

                case VideoSize.F1080p:
                {
                    maxRate = "1600k";
                    Tuple <int, int> finalSize = SizeHelper.GetSize(sourceFile.VideoWidth.Value, sourceFile.VideoHeight.Value, 1920, 1080);
                    size = $"{finalSize.Item1}:{finalSize.Item2}";
                    break;
                }

                default:
                    throw new InvalidOperationException("Format non reconnu.");
                }

                newEncodedFilePath = Path.ChangeExtension(TempFileManager.GetNewTempFilePath(), ".mp4");
                string arguments;
                if (VideoSettings.GpuEncodeMode)
                {
                    arguments = $"-y -hwaccel cuvid -vcodec h264_cuvid -vsync 0 -i {Path.GetFileName(sourceFilePath)} -vf \"scale_npp={size},format=yuv420p\" -b:v {maxRate} -maxrate {maxRate} -bufsize {maxRate} -vcodec h264_nvenc -acodec copy {Path.GetFileName(newEncodedFilePath)}";
                }
                else
                {
                    arguments = $"-y -i {Path.GetFileName(sourceFilePath)} -vf \"scale={size},format=yuv420p\" -vcodec libx264 -acodec aac {Path.GetFileName(newEncodedFilePath)}";
                }

                var ffmpegProcessManager = new FfmpegProcessManager(fileItem);
                ffmpegProcessManager.StartProcess(arguments, VideoSettings.EncodeTimeout);

                fileItem.FilePath = newEncodedFilePath;
                LogManager.AddEncodingMessage("OutputFileName " + Path.GetFileName(newEncodedFilePath) + " / FileSize " + fileItem.FileSize + " / Format " + videoSize, "End Encoding");

                fileItem.EncodeProgress = "100.00%";
                return(true);
            }
            catch (Exception ex)
            {
                LogManager.AddEncodingMessage("Video Duration " + fileItem.VideoDuration + " / FileSize " + fileItem.FileSize + " / Progress " + fileItem.EncodeProgress + " / Exception : " + ex, "Exception");
                fileItem.EncodeErrorMessage = "Exception";
                TempFileManager.SafeDeleteTempFile(newEncodedFilePath);
                fileItem.CleanFiles();
                return(false);
            }
        }