Exemplo n.º 1
0
        /// <summary>
        /// For easier reading I've put everything into 3 functions, rather than process classes.
        /// This should clarify more how everything works.
        /// </summary>
        /// <param name="pResourceHandler"></param>
        /// <param name="pFilePath"></param>
        /// <param name="pOutputFolder"></param>
        /// <param name="pAffinity"></param>
        /// <returns></returns>
        private static int ConvertAndCompressVideo(Cleanup.ResourceHandler pResourceHandler, string pFilePath, string pOutputFolder, int pAffinity)
        {
            ///////////////////////////// FIRST STEP IS TO CONVERT FILE /////////////////////////////
            string arguments = "";
            string delimiter = "&";

            string fileName   = Path.GetFileName(pFilePath);
            string rootFolder = Path.GetDirectoryName(pFilePath);

            arguments += "format" + delimiter + Settings.ConversionSettings.FORMAT + " ";
            arguments += "fileName" + delimiter + fileName + " ";
            arguments += "folderInput" + delimiter + rootFolder + " ";
            arguments += "folderOutput" + delimiter + pOutputFolder + " ";

            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName  = Settings.ConversionSettings.CONVERSION_PROCESS_NAME,
                    Arguments = arguments
                }
            };

            try
            {
                process.Start();
                process.ProcessorAffinity = (System.IntPtr)pAffinity;
                process.WaitForExit();
            }
            catch (Exception e)
            {
                string err = e.ToString();
                Console.WriteLine("Failed process image: " + err);
                return(-1);
            }

            ///////////////////////////// COMPRESS FILE /////////////////////////////
            string fileNoExt         = Path.GetFileNameWithoutExtension(pFilePath);
            string convertedFileName = "converted_" + fileNoExt + "." + Settings.ConversionSettings.FORMAT;

            pResourceHandler.AddForDeletionFile(Path.Combine(pOutputFolder, convertedFileName));

            string newFilePath = Path.GetDirectoryName(pFilePath);

            newFilePath = Path.Combine(newFilePath, Settings.MediaSettings.OUTPUT_FOLDER);
            newFilePath = Path.Combine(newFilePath, convertedFileName);

            string outputFilePath = Path.Combine(pOutputFolder, fileNoExt + "." + Settings.ConversionSettings.FORMAT);

            return(CompressVideo(pResourceHandler, newFilePath, outputFilePath, pAffinity));
        }
Exemplo n.º 2
0
        static int Main(string[] args)
        {
            //args = new string[1] {"Input/image/cpu.png"};
            //args = new string[1] { "Input/movie1/movie.mp4" };
            //args = new string[1] { "Input/movie2/movie.mov" };

            ///////////////////////////// CHECK FOR EMPTY ARGUMENTS /////////////////////////////
            if (args.Length == 0)
            {
                Console.WriteLine("No arguments.");
                Console.ReadLine();
                return(-1);
            }

            ///////////////////////////// INITIIALIZE GLOBAL SETTINGS /////////////////////////////
            GlobalTools.LogCodes.InitializeDescriptions();

            Settings.SettingsHelper.InitializeSettingsFromAppConfig();

            ///////////////////////////// CONSTRUCT FILE PATHS /////////////////////////////
            string filePath = "";

            for (int i = 0; i < args.Length; i++)
            {
                filePath += args[i] + " ";
            }

            string folderInput  = Path.GetDirectoryName(filePath);
            string folderOutput = Path.Combine(folderInput, Settings.MediaSettings.OUTPUT_FOLDER);
            string fileName     = Path.GetFileName(filePath);

            ///////////////////////////// CHECK IF FILES/FOLDERS EXIST /////////////////////////////
            if (File.Exists(filePath) == false)
            {
                Console.WriteLine("File does not exist.");
                Console.WriteLine("Path: " + filePath);
                Console.ReadLine();
                return(-1);
            }

            ///////////////////////////// DETERMINE TYPE AND RETURN IF UNSUPPORTED /////////////////////////////
            string extension = Path.GetExtension(filePath);

            extension = extension.Replace(".", "");
            extension = extension.Replace(" ", "");

            int type = IsSupportedFormat(extension);

            if (type < 0)
            {
                return(-1);
            }

            ///////////////////////////// CREATE FOLDER IF NOT EXIST /////////////////////////////
            if (Directory.Exists(folderOutput) == false)
            {
                Directory.CreateDirectory(folderOutput);
            }

            ///////////////////////////// ADD FILES TO BE DELETED WHEN FINISHED /////////////////////////////
            Cleanup.ResourceHandler handler = new Cleanup.ResourceHandler();
            handler.AddForDeletionFile(filePath);

            ///////////////////////////// HERE STARTS THE PROCESSES /////////////////////////////

            int resultCode = 0;
            int affinity   = 0;

            if (type == 1)
            {
                bool isSameFormat = (extension == Settings.ConversionSettings.FORMAT);

                for (int i = 0; i < Settings.MediaSettings.CORES_VIDEO.Length; i++)
                {
                    int core = Settings.MediaSettings.CORES_VIDEO[i];
                    affinity = affinity | (1 << core - 1);
                }

                ///////////////////////////// IT IS SAME FORMAT, SKIP CONVERSION /////////////////////////////
                if (isSameFormat)
                {
                    string outputFilePath = Path.Combine(folderOutput, fileName);
                    resultCode = CompressVideo(handler, filePath, outputFilePath, affinity);
                }
                ///////////////////////////// DO CONVERSION AND COMPRESSION /////////////////////////////
                else
                {
                    resultCode = ConvertAndCompressVideo(handler, filePath, folderOutput, affinity);
                }
            }
            else if (type == 2)
            {
                for (int i = 0; i < Settings.MediaSettings.CORES_IMAGE.Length; i++)
                {
                    int core = Settings.MediaSettings.CORES_IMAGE[i];
                    affinity = affinity | (1 << core - 1);
                }

                resultCode = ProcessImage(handler, filePath, folderOutput, affinity);
            }


            ///////////////////////////// DELETE INPUT AND OTHER TEMPORARY CREATED FILES. /////////////////////////////
            //handler.SafeRelease();

            Console.WriteLine("Program ended.");
            Console.ReadLine();
            return(0);
        }