Example #1
0
        private void OnRecordingStopped(object sender, StoppedEventArgs err)
        {
            if (err.Exception != null)
            {
                InfoMessage?.Invoke(this, $"Ошибка: {err.Exception.Message}");
            }

            ffmpegProcess?.StandardOutput.Close();
            ffmpegProcess?.StandardInput.Close();
            ffmpegProcess?.Kill();

            _audioCapture.RecordingStopped -= OnRecordingStopped;
            _audioCapture.DataAvailable    -= OnDataAvailable;


            _audioCapture.Dispose();
            _audioCapture            = null;
            _threadSafeBoolBackValue = 0;

            Task.Run(() => { _transportService.SendFinalData(); }).Wait();
            Task.Run(() => { _transportService.CloseConnection(); }).Wait();
            InfoMessage?.Invoke(this, "Запись остановлена");
            RecordLevel?.Invoke(this, 0.0F);
            RecordStopped?.Invoke(this, EventArgs.Empty);
        }
        public async Task TranscribeFile(string filePath)
        {
            InfoMessage.Invoke(this, "Обработка файла...");
            _cancellationTokenSource = new CancellationTokenSource();
            CancellationToken cancellationToken = _cancellationTokenSource.Token;

            var process = new Process();

            process.StartInfo.FileName               = "ffmpeg.exe";
            process.StartInfo.Arguments              = $"-i \"{filePath}\" -ar 8000 -ac 1 -f s16le -";
            process.StartInfo.RedirectStandardInput  = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError  = true;
            process.StartInfo.UseShellExecute        = false;
            process.StartInfo.CreateNoWindow         = true;
            process.Start();


            var task = Task.Run(() =>
            {
                FFmpegPercentageParser ffmpegParser = new FFmpegPercentageParser();
                string line;
                int previousPercent;
                while ((line = process.StandardError.ReadLine()) != null && !cancellationToken.IsCancellationRequested)
                {
                    ffmpegParser.Parse(line);
                    if (ffmpegParser.GetPercentCompleted() is int currentPercent)
                    {
                        previousPercent = currentPercent;
                        PercentageTranscribed?.Invoke(this, currentPercent);
                    }
                }
            });

            var buffer = new byte[8000];
            int read;

            while ((read = process.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0 && !cancellationToken.IsCancellationRequested)
            {
                await _transportService.SendData(buffer, read);
            }
            await _transportService.SendFinalData();

            await _transportService.CloseConnection();

            process.Kill();
            _cancellationTokenSource.Dispose();
            _cancellationTokenSource = null;
            InfoMessage.Invoke(this, "Операция завершена");
        }