コード例 #1
0
        public void GetVideoThumbnail(string inputFile, string outputFile, float?frameTime)
        {
            Media input = new Media {
                Filename = inputFile
            };
            Media output = new Media {
                Filename = outputFile,
                Format   = "mjpeg"
            };
            ConvertSettings settings = new ConvertSettings {
                VideoFrameCount = 1,
                Seek            = frameTime,
                MaxDuration     = 1f
            };

            this.ConvertMedia(input, output, settings);
        }
コード例 #2
0
        protected string ComposeFFMpegCommandLineArgs(string inputFile, string inputFormat, string outputFile, string outputFormat, ConvertSettings settings)
        {
            //return "-y -loglevel info -f dshow -i audio=\"麦克风 (Realtek High Definition Audio)\" -f gdigrab  -draw_mouse 1 -i \"desktop\" -ar 44100 -r 30 -s hd1080  -qscale 15 udp://127.0.0.1:7777";
            //return "-y -f dshow -i audio=\"您听到的声音 (Sound Blaster Recon3Di)\" -f gdigrab -draw_mouse 1 -video_size 1920x1080 -i desktop -vcodec h264 " + this.CommandArgParameter(outputFile);

            StringBuilder builder = new StringBuilder();

            if (!string.IsNullOrEmpty(settings.AudioDevice))
            {
                builder.AppendFormat("-f dshow -i audio=\"{0}\"", settings.AudioDevice);
            }

            if (settings.AppendSilentAudioStream)
            {
                builder.Append(" -f lavfi -i aevalsrc=0 ");
            }
            if (settings.Seek.HasValue)
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, " -ss {0}", new object[] { settings.Seek });
            }
            if (inputFormat != null)
            {
                builder.Append(" -f " + inputFormat);
            }
            if (settings.CustomInputArgs != null)
            {
                builder.AppendFormat(" {0} ", settings.CustomInputArgs);
            }
            StringBuilder outputArgs = new StringBuilder();

            this.ComposeFFMpegOutputArgs(outputArgs, outputFormat, settings);
            if (settings.AppendSilentAudioStream)
            {
                outputArgs.Append(" -shortest ");
            }
            return(string.Format("-y -loglevel {4} {0} -i {1} {2} {3}", new object[] { builder.ToString(), this.CommandArgParameter(inputFile), outputArgs.ToString(), this.CommandArgParameter(outputFile), this.LogLevel }));
        }
コード例 #3
0
        private ConvertLiveMediaTask CreateLiveMediaTask(string toolArgs, Stream inputStream, Stream outputStream, ConvertSettings settings)
        {
            FFMpegProgress progress = new FFMpegProgress(new Action <ConvertProgressEventArgs>(this.OnConvertProgress), this.ConvertProgress != null);

            if (settings != null)
            {
                progress.Seek        = settings.Seek;
                progress.MaxDuration = settings.MaxDuration;
            }
            return(new ConvertLiveMediaTask(this, toolArgs, inputStream, outputStream, progress));
        }
コード例 #4
0
        public void ConvertMedia(string inputFile, string inputFormat, string outputFile, string outputFormat, ConvertSettings settings)
        {
            if (inputFile == null)
            {
                throw new ArgumentNullException("inputFile");
            }
            if (outputFile == null)
            {
                throw new ArgumentNullException("outputFile");
            }
            if ((File.Exists(inputFile) && string.IsNullOrEmpty(Path.GetExtension(inputFile))) && (inputFormat == null))
            {
                throw new Exception("Input format is required for file without extension");
            }
            if (string.IsNullOrEmpty(Path.GetExtension(outputFile)) && (outputFormat == null))
            {
                throw new Exception("Output format is required for file without extension");
            }
            Media input = new Media {
                Filename = inputFile,
                Format   = inputFormat
            };
            Media output = new Media {
                Filename = outputFile,
                Format   = outputFormat
            };

            this.ConvertMedia(input, output, settings ?? new ConvertSettings());
        }
コード例 #5
0
        internal void ConvertMedia(Media input, Media output, ConvertSettings settings)
        {
            this.EnsureFFMpegLibs();
            string filename = input.Filename;

            if (filename == null)
            {
                filename = Path.GetTempFileName();
                using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    this.CopyStream(input.DataStream, stream, 0x40000);
                }
            }
            string path = output.Filename;

            if (path == null)
            {
                path = Path.GetTempFileName();
            }
            if (((output.Format == "flv") || (Path.GetExtension(path).ToLower() == ".flv")) && !settings.AudioSampleRate.HasValue)
            {
                settings.AudioSampleRate = 0xac44;
            }
            try
            {
                string fFMpegExePath = this.GetFFMpegExePath();
                if (!File.Exists(fFMpegExePath))
                {
                    throw new FileNotFoundException("Cannot find ffmpeg tool: " + fFMpegExePath);
                }
                string           arguments = this.ComposeFFMpegCommandLineArgs(filename, input.Format, path, output.Format, settings);
                ProcessStartInfo startInfo = new ProcessStartInfo(fFMpegExePath, arguments)
                {
                    WindowStyle            = ProcessWindowStyle.Hidden,
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    WorkingDirectory       = Path.GetDirectoryName(this.FFMpegToolPath),
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true
                };
                this.InitStartInfo(startInfo);
                if (this.FFMpegProcess != null)
                {
                    throw new InvalidOperationException("FFMpeg process is already started");
                }
                this.FFMpegProcess = Process.Start(startInfo);
                if (this.FFMpegProcessPriority != ProcessPriorityClass.Normal)
                {
                    this.FFMpegProcess.PriorityClass = this.FFMpegProcessPriority;
                }
                string         lastErrorLine  = string.Empty;
                FFMpegProgress ffmpegProgress = new FFMpegProgress(new Action <ConvertProgressEventArgs>(this.OnConvertProgress), this.ConvertProgress != null);
                if (settings != null)
                {
                    ffmpegProgress.Seek        = settings.Seek;
                    ffmpegProgress.MaxDuration = settings.MaxDuration;
                }
                this.FFMpegProcess.ErrorDataReceived += delegate(object o, DataReceivedEventArgs args) {
                    if (args.Data != null)
                    {
                        lastErrorLine = args.Data;
                        ffmpegProgress.ParseLine(args.Data);
                        this.FFMpegLogHandler(args.Data);
                    }
                };
                this.FFMpegProcess.OutputDataReceived += delegate(object o, DataReceivedEventArgs args) {
                };
                this.FFMpegProcess.BeginOutputReadLine();
                this.FFMpegProcess.BeginErrorReadLine();
                this.WaitFFMpegProcessForExit();
                if (this.FFMpegProcess.ExitCode != 0)
                {
                    throw new FFMpegException(this.FFMpegProcess.ExitCode, lastErrorLine);
                }
                this.FFMpegProcess.Close();
                this.FFMpegProcess = null;
                ffmpegProgress.Complete();
                if (output.Filename == null)
                {
                    using (FileStream stream2 = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        this.CopyStream(stream2, output.DataStream, 0x40000);
                    }
                }
            }
            catch (Exception)
            {
                this.EnsureFFMpegProcessStopped();
                throw;
            }
            finally
            {
                if (((filename != null) && (input.Filename == null)) && File.Exists(filename))
                {
                    File.Delete(filename);
                }
                if (((path != null) && (output.Filename == null)) && File.Exists(path))
                {
                    File.Delete(path);
                }
            }
        }
コード例 #6
0
        public ConvertLiveMediaTask ConvertLiveMedia(string inputSource, string inputFormat, Stream outputStream, string outputFormat, ConvertSettings settings)
        {
            this.EnsureFFMpegLibs();
            string toolArgs = this.ComposeFFMpegCommandLineArgs(inputSource, inputFormat, "-", outputFormat, settings);

            return(this.CreateLiveMediaTask(toolArgs, null, outputStream, settings));
        }
コード例 #7
0
 public ConvertLiveMediaTask ConvertLiveMedia(string inputFormat, Stream outputStream, string outputFormat, ConvertSettings settings)
 {
     return(this.ConvertLiveMedia((Stream)null, inputFormat, outputStream, outputFormat, settings));
 }