Exemplo n.º 1
0
 private async void RunSimpleButton_Click(object sender, RoutedEventArgs e)
 {
     if (Validate())
     {
         ProcessStartOptions Options = new ProcessStartOptions(FFmpegDisplayMode.Interface, "Encoding to H264/AAC (Simple)");
         string Src = SourceTextBox.Text;
         string Dst = DestinationTextBox.Text;
         await Task.Run(() => {
             MediaEncoder.Encode(Src, "h264", "aac", null, Dst, Options);
         });
     }
 }
        /// <summary>
        /// Encodes specified audio file according to settings. The script file must already be written.
        /// </summary>
        /// <param name="settings">An object containing the encoding settings.</param>
        /// <returns>The endoding completion status..</returns>
        public static CompletionStatus EncodeAudio(MediaEncoderSettings settings)
        {
            CompletionStatus    Result  = CompletionStatus.Success;
            string              WavFile = PathManager.GetAudioFile(settings.JobIndex, AudioActions.Wav);
            ProcessStartOptions Options = new ProcessStartOptions(settings.JobIndex, "Exporting Audio", false).TrackProcess(settings);

            if (!File.Exists(WavFile))
            {
                EncoderBusiness.SaveAudioToWav(settings, WavFile, Options);
                if (settings.CompletionStatus == CompletionStatus.Cancelled)
                {
                    File.Delete(WavFile);
                    return(CompletionStatus.Cancelled);
                }
                if (!File.Exists(WavFile))
                {
                    settings.Cancel();
                    return(CompletionStatus.Error);
                }
            }

            string DestFile = PathManager.GetAudioFile(settings.JobIndex, settings.AudioAction);

            if (!File.Exists(DestFile))
            {
                Options.Title = "Encoding Audio";
                if (settings.AudioAction == AudioActions.Opus)
                {
                    string        Args   = string.Format(@"--bitrate {0} ""{1}"" ""{2}""", settings.AudioQuality, WavFile, DestFile);
                    FFmpegProcess Worker = new FFmpegProcess(Options);
                    Result = Worker.Run("Encoder\\opusenc.exe", Args);
                }
                else if (settings.AudioAction == AudioActions.Aac || settings.AudioAction == AudioActions.Flac)
                {
                    Result = MediaEncoder.Encode(WavFile, null,
                                                 settings.AudioAction == AudioActions.Flac ? "flac" : "aac",
                                                 string.Format("-b:a {0}k", settings.AudioQuality),
                                                 DestFile,
                                                 Options);
                }
            }

            if (Result != CompletionStatus.Success || !File.Exists(DestFile))
            {
                File.Delete(DestFile);
                settings.Cancel();
            }
            return(Result);
        }
Exemplo n.º 3
0
        private CompletionStatus ExecuteComplex(string src, string dst)
        {
            string DstEncode  = GetPathWithoutExtension(dst) + "_.mp4";
            string DstExtract = GetPathWithoutExtension(dst) + "_.mkv";
            string DstAac     = GetPathWithoutExtension(dst) + "_.aac";

            jobId++;
            CompletionStatus Result;

            FFmpegConfig.UserInterfaceManager.Start(jobId, "Encoding to H264/AAC (Complex)");

            ProcessStartOptions OptionsMain = new ProcessStartOptions(jobId, "", true);
            FFmpegProcess       ProcessMain = null;

            OptionsMain.Started += (sender, e) => {
                ProcessMain = e.Process;
            };
            Task <CompletionStatus> TaskMain = Task.Run(() => MediaEncoder.Encode(src, "h264", null, "", DstEncode, OptionsMain));

            ProcessStartOptions Options = new ProcessStartOptions(jobId, "Extracting Audio", false);

            Result = MediaMuxer.ExtractAudio(src, DstExtract, Options);
            if (Result == CompletionStatus.Success)
            {
                Options.Title = "Encoding Audio";
                Result        = MediaEncoder.Encode(DstExtract, null, "aac", null, DstAac, Options);
            }

            if (Result != CompletionStatus.Success)
            {
                ProcessMain?.Cancel();
            }

            TaskMain.Wait();
            CompletionStatus Result2 = TaskMain.Result;

            if (Result == CompletionStatus.Success && Result2 == CompletionStatus.Success)
            {
                Options.Title = "Muxing Audio and Video";
                Result        = MediaMuxer.Muxe(DstEncode, DstAac, dst, Options);
            }

            File.Delete(DstEncode);
            File.Delete(DstExtract);
            File.Delete(DstAac);
            FFmpegConfig.UserInterfaceManager.Stop(jobId);
            return(Result);
        }
        /// <summary>
        /// Encodes specified video file according to settings. The script file must already be written.
        /// </summary>
        /// <param name="settings">An object containing the encoding settings.</param>
        /// <param name="frameCount">The amount of frames to process.</param>
        /// <param name="totalFrameCount">If encoding in various segments, the total amount of frames in the script.</param>
        /// <returns>The endoding completion status..</returns>
        public static CompletionStatus EncodeVideo(MediaEncoderSettings settings, long frameCount, long totalFrameCount)
        {
            CompletionStatus Result = CompletionStatus.None;

            File.Delete(settings.OutputFile);
            string Codec = "", Args = "";

            if (settings.VideoAction == VideoAction.x264)
            {
                Codec = "libx264";
            }
            else if (settings.VideoAction == VideoAction.x265)
            {
                Codec = "libx265";
                Args  = string.Format("-preset {0} -crf {1}", settings.EncodePreset, settings.EncodeQuality);
            }
            else if (settings.VideoAction == VideoAction.Avi)
            {
                Codec = "huffyuv";
            }
            else if (settings.VideoAction == VideoAction.AviUtVideo)
            {
                Codec = "utvideo";
            }
            else if (settings.VideoAction == VideoAction.xvid)
            {
                Codec = "xvid";
            }
            else if (settings.VideoAction == VideoAction.x264_10bit)
            {
                Args = string.Format("--preset {0} --crf {1}", settings.EncodePreset, settings.EncodeQuality);
            }

            if (settings.ParallelProcessing > 1)
            {
                Args += settings.VideoAction == VideoAction.x264_10bit ? " --threads 4" : " -threads 4";
            }

            ProcessStartOptions Options = new ProcessStartOptions(settings.JobIndex, "Processing Video", true).TrackProcess(settings);

            // Options.FrameCount = AvisynthTools.GetFrameCount(settings.ScriptFile, new ProcessStartOptions(settings.JobIndex, "Getting Frame Count", false).TrackProcess(settings));
            Options.FrameCount      = frameCount;
            Options.ResumePos       = settings.ResumePos;
            Options.TotalFrameCount = totalFrameCount;
            if (settings.CompletionStatus == CompletionStatus.Success)
            {
                string JobScript = settings.OutputScriptFile;
                File.Delete(JobScript);
                File.Copy(settings.ScriptFile, JobScript);
                EditStartPosition(JobScript, settings.ResumePos, settings.ResumePos + frameCount - 1);
                if (settings.VideoAction == VideoAction.x264_10bit)
                {
                    Result = EncodeX264_10bit(JobScript, Args, settings.OutputFile, Options);
                }
                else
                {
                    Result = MediaEncoder.Encode(JobScript, Codec, null, Args, settings.OutputFile, Options);
                }
                File.Delete(JobScript);
            }
            else
            {
                Result = settings.CompletionStatus;
            }
            return(Result);
        }