2017-03-20 114 views
0

FFmpeg在解碼小的mp3文件時凍結。 Thecrash發生在大約8或9個數據緩衝區之後。我不知道發生了什麼。Ffmpeg在解碼時死機

  await e.Channel.SendMessage("Playing"); 
      var server = e.Server; 
      var voiceChannel = client.FindServers(server.Name).FirstOrDefault().VoiceChannels.FirstOrDefault(); 
      var _vClient = await client.GetService<AudioService>() 
       .Join(voiceChannel); 

      var pathOrUrl = "a.mp3"; 

      var process = Process.Start(new ProcessStartInfo 
      { // FFmpeg requires us to spawn a process and hook into its stdout, so we will create a Process 
       FileName = "ffmpeg", 
       Arguments = $"-i {pathOrUrl} " + // Here we provide a list of arguments to feed into FFmpeg. -i means the location of the file/URL it will read from 
          "-f s16le -ar 48000 -ac 1 pipe:1", // Next, we tell it to output 16-bit 48000Hz PCM, over 2 channels, to stdout. 
       UseShellExecute = false, 
       RedirectStandardOutput = true // Capture the stdout of the process 
      }); 
      Thread.Sleep(2000); // Sleep for a few seconds to FFmpeg can start processing data. 

      int blockSize = 3840; // The size of bytes to read per frame; 1920 for mono 
      byte[] buffer = new byte[blockSize]; 
      int byteCount; 

      while (true) // Loop forever, so data will always be read 
      { 
       byteCount = process.StandardOutput.BaseStream // Access the underlying MemoryStream from the stdout of FFmpeg 
         .Read(buffer, 0, blockSize); // Read stdout into the buffer 

       if (byteCount == 0) { // FFmpeg did not output anything 
        Console.WriteLine("Finished"); 
        break; 
        }// Break out of the while(true) loop, since there was nothing to read. 

       _vClient.Send(buffer, 0, byteCount); // Send our data to Discord 
      } 
      _vClient.Wait(); 
+1

你從哪裏得到塊大小?對於48khz單聲道PCM,ffmpeg默認顯示2048的幀大小。 – Mulvya

+1

首先,確保您可以將所有預期的輸出寫入本地文件。如果這可行,請檢查您的管道。 –

+0

@AlexCohn我對這個新手有一點點興趣,但是到目前爲止已經設法完成所有工作。 –

回答

0

我錯過了不和諧的一面,使它看起來就像ffmpeg凍結的依賴。

1

你的論點:

-f s16le -ar 48000 -ac 1 pipe:1 

這些都是我用轉換的論點MP3:

-acodec libmp3lame -ar 48000 -ac 2 -map 0:a:0? 

我相信-ac 1爲單聲道,-ac 2是立體聲
https://trac.ffmpeg.org/wiki/AudioChannelManipulation#monostereo

什麼時候 我用你的參數-f s16le來轉換一個mp3文件出來腐敗和靜音。如果排除它,我認爲它仍然會自動轉換爲16位。

你還可以嘗試:

-f pcm_s16le 

也許這些細節會導致幫你解決問題。

+0

圖例,我會在我回家時嘗試。 –

+2

'-f s16le'是原始PCM,即沒有任何頭部的WAV/AIFF的有效載荷.OP正試圖解碼MP3,而不是編碼成MP3。 – Mulvya

+1

@Mulvya我看到了,我會再看看一個解決方案。 –