示例#1
0
        /// <summary>
        /// Convert a video do a different format.
        /// </summary>
        /// <param name="source">Input video source.</param>
        /// <param name="output">Output information.</param>
        /// <param name="type">Target conversion video type.</param>
        /// <param name="speed">Conversion target speed/quality (faster speed = lower quality).</param>
        /// <param name="size">Video size.</param>
        /// <param name="audioQuality">Conversion target audio quality.</param>
        /// <param name="multithreaded">Is encoding multithreaded.</param>
        /// <returns>Output video information.</returns>
        public VideoInfo Convert(
            VideoInfo source,
            FileInfo output,
            VideoType type = VideoType.Mp4,
            Speed speed    =
            Speed.SuperFast,
            VideoSize size =
            VideoSize.Original,
            AudioQuality audioQuality = AudioQuality.Normal,
            bool multithreaded        = false)
        {
            FFMpegHelper.ConversionExceptionCheck(source.ToFileInfo(), output);
            FFMpegHelper.ExtensionExceptionCheck(output, FileExtension.ForType(type));
            FFMpegHelper.ConversionSizeExceptionCheck(source);

            string args = "";

            var scale = VideoSize.Original == size ? 1 :
                        (double)source.Height / (int)size;

            var outputSize = new Size(
                (int)(source.Width / scale),
                (int)(source.Height / scale)
                );

            if (outputSize.Width % 2 != 0)
            {
                outputSize.Width += 1;
            }

            switch (type)
            {
            case VideoType.Mp4:

                args = ArgumentsStringifier.Input(source) +
                       ArgumentsStringifier.Threads(multithreaded) +
                       ArgumentsStringifier.Scale(outputSize) +
                       ArgumentsStringifier.Video(VideoCodec.LibX264, 2400) +
                       ArgumentsStringifier.Speed(speed) +
                       ArgumentsStringifier.Audio(AudioCodec.Aac, audioQuality) +
                       ArgumentsStringifier.Output(output);
                break;

            case VideoType.Ogv:
                args = ArgumentsStringifier.Input(source) +
                       ArgumentsStringifier.Threads(multithreaded) +
                       ArgumentsStringifier.Scale(outputSize) +
                       ArgumentsStringifier.Video(VideoCodec.LibTheora, 2400) +
                       ArgumentsStringifier.Speed(16) +
                       ArgumentsStringifier.Audio(AudioCodec.LibVorbis, audioQuality) +
                       ArgumentsStringifier.Output(output);

                break;

            case VideoType.Ts:
                args = ArgumentsStringifier.Input(source) +
                       ArgumentsStringifier.Copy() +
                       ArgumentsStringifier.BitStreamFilter(Channel.Video, Filter.H264_Mp4ToAnnexB) +
                       ArgumentsStringifier.ForceFormat(VideoCodec.MpegTs) +
                       ArgumentsStringifier.Output(output);
                break;
            }

            if (!RunProcess(args, output))
            {
                throw new FFMpegException(FFMpegExceptionType.Conversion, $"The video could not be converted to {Enum.GetName(typeof(VideoType), type)}");
            }

            return(new VideoInfo(output));
        }