Exemplo n.º 1
0
        /// <summary>
        ///     Saves a 'png' thumbnail to an in-memory bitmap
        /// </summary>
        /// <param name="source">Source video file.</param>
        /// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
        /// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
        /// <returns>Bitmap with the requested snapshot.</returns>
        public static Bitmap Snapshot(MediaAnalysis source, Size?size = null, TimeSpan?captureTime = null)
        {
            captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);

            size = PrepareSnapshotSize(source, size);

            using var ms = new MemoryStream();
            FFMpegArguments
            .FromInputFiles(source.Path)
            .WithVideoCodec(VideoCodec.Png)
            .WithFrameOutputCount(1)
            .Resize(size)
            .Seek(captureTime)
            .ForceFormat("rawvideo")
            .OutputToPipe(new StreamPipeSink(ms))
            .ProcessSynchronously();

            ms.Position = 0;
            return(new Bitmap(ms));
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Saves a 'png' thumbnail from the input video to drive
        /// </summary>
        /// <param name="source">Source video analysis</param>
        /// <param name="output">Output video file path</param>
        /// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
        /// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
        /// <returns>Bitmap with the requested snapshot.</returns>
        public static bool Snapshot(MediaAnalysis source, string output, Size?size = null, TimeSpan?captureTime = null)
        {
            captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);

            if (Path.GetExtension(output) != FileExtension.Png)
            {
                output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
            }

            size = PrepareSnapshotSize(source, size);

            return(FFMpegArguments
                   .FromInputFiles(source.Path)
                   .WithVideoCodec(VideoCodec.Png)
                   .WithFrameOutputCount(1)
                   .Resize(size)
                   .Seek(captureTime)
                   .OutputToFile(output)
                   .ProcessSynchronously());
        }
Exemplo n.º 3
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 static bool Convert(
            MediaAnalysis source,
            string output,
            ContainerFormat format,
            Speed speed               = Speed.SuperFast,
            VideoSize size            = VideoSize.Original,
            AudioQuality audioQuality = AudioQuality.Normal,
            bool multithreaded        = false)
        {
            FFMpegHelper.ExtensionExceptionCheck(output, format.Extension);
            FFMpegHelper.ConversionSizeExceptionCheck(source);

            var scale      = VideoSize.Original == size ? 1 : (double)source.PrimaryVideoStream.Height / (int)size;
            var outputSize = new Size((int)(source.PrimaryVideoStream.Width / scale), (int)(source.PrimaryVideoStream.Height / scale));

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

            return(format.Name switch
            {
                "mp4" => FFMpegArguments
                .FromInputFiles(true, source.Path)
                .UsingMultithreading(multithreaded)
                .WithVideoCodec(VideoCodec.LibX264)
                .WithVideoBitrate(2400)
                .Scale(outputSize)
                .WithSpeedPreset(speed)
                .WithAudioCodec(AudioCodec.Aac)
                .WithAudioBitrate(audioQuality)
                .OutputToFile(output)
                .ProcessSynchronously(),
                "ogv" => FFMpegArguments
                .FromInputFiles(true, source.Path)
                .UsingMultithreading(multithreaded)
                .WithVideoCodec(VideoCodec.LibTheora)
                .WithVideoBitrate(2400)
                .Scale(outputSize)
                .WithSpeedPreset(speed)
                .WithAudioCodec(AudioCodec.LibVorbis)
                .WithAudioBitrate(audioQuality)
                .OutputToFile(output)
                .ProcessSynchronously(),
                "mpegts" => FFMpegArguments
                .FromInputFiles(true, source.Path)
                .CopyChannel()
                .WithBitStreamFilter(Channel.Video, Filter.H264_Mp4ToAnnexB)
                .ForceFormat(VideoType.Ts)
                .OutputToFile(output)
                .ProcessSynchronously(),
                "webm" => FFMpegArguments
                .FromInputFiles(true, source.Path)
                .UsingMultithreading(multithreaded)
                .WithVideoCodec(VideoCodec.LibVpx)
                .WithVideoBitrate(2400)
                .Scale(outputSize)
                .WithSpeedPreset(speed)
                .WithAudioCodec(AudioCodec.LibVorbis)
                .WithAudioBitrate(audioQuality)
                .OutputToFile(output)
                .ProcessSynchronously(),
                _ => throw new ArgumentOutOfRangeException(nameof(format))
            });