コード例 #1
0
        private void ProcessVoice(Message message)
        {
            Task.Run(async() =>
            {
                var fileName = Path.ChangeExtension(Path.GetTempFileName(), ".mp4");
                var fs       = File.Create(fileName);

                try
                {
                    Notificator.VoiceHandled(message);

                    var file = await _client.GetFileAsync(message.Voice.FileId);
                    await _client.DownloadFileAsync(file.FilePath, fs);
                    fs.Close();

                    var mediaInfo = FFmpeg.GetMediaInfo(fileName).Result;

                    IStream audioStream = mediaInfo.AudioStreams.FirstOrDefault()
                                          ?.SetCodec(AudioCodec.pcm_s16le)
                                          ?.SetChannels(1)
                                          ?.SetSampleRate(16000);

                    var outputPath = @"C:\test\.test.wav"; //Path.ChangeExtension(Path.GetTempFileName(), ".wav");

                    await FFmpeg.Conversions.New().AddStream(audioStream).SetOutput(outputPath).Start();

                    //var text = await _voiceСonverter.ConvertAsync(fs.Name);
                    //Notificator.VoiceConverted(message, text.Length);

                    //await _client.SendTextMessageAsync(message.Chat.Id, PhrasesHelper.RandomVoice + $"\n{text}.", replyToMessageId: message.MessageId);
                }
                catch (Exception exc)
                {
                    Notificator.Error(exc.Message);
                    await _client.SendTextMessageAsync(message.Chat.Id,
                                                       message.GetFromFirstName() + _settings.Messages.ErrorUploadMessage,
                                                       replyToMessageId: message.MessageId);
                }
                finally
                {
                    File.Delete(fs.Name);
                }
            });
        }