Beispiel #1
0
        private void OnException(List <string> messages, FFmpegParameters parameters, int exitCode, Exception caughtException)
        {
            var exceptionMessage = GetExceptionMessage(messages);
            var exception        = new FFmpegException(exceptionMessage, caughtException, exitCode);

            OnConversionError(new ConversionErrorEventArgs(exception, parameters.InputFile, parameters.OutputFile));
        }
Beispiel #2
0
 public ConversionErrorEventArgs(FFmpegException exception, MediaFile input, MediaFile output, List <string> messages, ConversionOptions options = null)
 {
     Exception = exception;
     Input     = input;
     Output    = output;
     Messages  = messages;
     Options   = options;
 }
Beispiel #3
0
        private static unsafe MediaIO FromStream(Stream stream, int writeFlag, int bufferSize = 32768)
        {
            byte *buffer = (byte *)av_malloc((ulong)bufferSize);

            if (buffer == null)
            {
                throw FFmpegException.NoMemory("Failed to alloc MediaIO buffer");
            }
            var callbackObject = new
            {
                ReadPacket  = new avio_alloc_context_read_packet(Read),
                WritePacket = new avio_alloc_context_write_packet(Write),
                Seek        = new avio_alloc_context_seek(Seek)
            };
            AVIOContext *ctx = avio_alloc_context(buffer, bufferSize, writeFlag,
                                                  opaque: null,
                                                  read_packet: callbackObject.ReadPacket,
                                                  write_packet: callbackObject.WritePacket,
                                                  seek: callbackObject.Seek);

            if (ctx == null)
            {
                throw FFmpegException.NoMemory("Failed to alloc AVIOContext");
            }

            return(new StreamMediaIO(ctx, isOwner: true, callbackObject));

            int Read(void *opaque, byte *buffer, int length)
            {
                int c = stream.Read(new Span <byte>(buffer, length));

                return(c == 0 ? AVERROR_EOF : c);
            }

            int Write(void *opaque, byte *buffer, int length)
            {
                stream.Write(new Span <byte>(buffer, length));
                return(length);
            }

            long Seek(void *opaque, long position, int origin) => (MediaIOSeek)origin switch
            {
                MediaIOSeek.Begin => stream.Seek(position, SeekOrigin.Begin),
                MediaIOSeek.Current => stream.Seek(position, SeekOrigin.Current),
                MediaIOSeek.End => stream.Seek(position, SeekOrigin.End),
                MediaIOSeek.Size => stream.Length,
                _ => throw new NotSupportedException(),
            };
        }
Beispiel #4
0
        private async Task ExecuteAsync(ProcessStartInfo startInfo, FFmpegParameters parameters, CancellationToken cancellationToken = default(CancellationToken))
        {
            var       messages        = new List <string>();
            Exception caughtException = null;

            using (var ffmpegProcess = Process.Start(startInfo))
            {
                if (ffmpegProcess == null)
                {
                    throw new InvalidOperationException(Resources.Resources.Exceptions_FFmpeg_Process_Not_Running);
                }

                ffmpegProcess.ErrorDataReceived += (sender, e) => OnData(new ConversionDataEventArgs(e.Data, parameters.InputFile, parameters.OutputFile));
                ffmpegProcess.ErrorDataReceived += (sender, e) => { FFmpegProcessOnErrorDataReceived(e, parameters, ref caughtException, messages); };

                ffmpegProcess.BeginErrorReadLine();
                await ffmpegProcess.WaitForExitAsync(cancellationToken);

                if (ffmpegProcess.ExitCode != 0 || caughtException != null)
                {
                    try
                    {
                        ffmpegProcess.Kill();
                    }
                    catch (InvalidOperationException)
                    {
                        // swallow exceptions that are thrown when killing the process,
                        // one possible candidate is the application ending naturally before we get a chance to kill it
                    }
                    catch (Win32Exception)
                    {
                    }

                    var exceptionMessage = GetExceptionMessage(messages);
                    var exception        = new FFmpegException(exceptionMessage, caughtException, ffmpegProcess.ExitCode);
                    OnConversionError(new ConversionErrorEventArgs(exception, parameters.InputFile, parameters.OutputFile));
                }
                else
                {
                    OnConversionCompleted(new ConversionCompleteEventArgs(parameters.InputFile, parameters.OutputFile));
                }
            }
        }
 public ConversionErrorEventArgs(FFmpegException exception, MediaFile input, MediaFile output)
 {
     Exception = exception;
     Input     = input;
     Output    = output;
 }
 public ConversionErrorEventArgs(FFmpegException exception, IInputArgument input, IOutputArgument output)
 {
     Exception = exception;
     Input     = input;
     Output    = output;
 }