2012-08-01 2 views
1

wma 스트림을 mp3 스트림으로 실시간 변환 할 수 있습니까?wma 스트림을 C# 및 ffmpeg로 mp3 스트림으로 변환

나는이 같은 일을하고 있지만 행운을 가지고 있지 시도 : 내가 뭘하려고 오전 (MP3의 바이트 블록에 WMA의 바이트 블록 변환)도 가능하면



    WebRequest request = WebRequest.Create(wmaUrl); 
    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
    int block = 32768; 
    byte[] buffer = new byte[block]; 

    Process p = new Process(); 
    p.StartInfo.UseShellExecute = false; 
    p.StartInfo.ErrorDialog = false; 
    p.StartInfo.RedirectStandardInput = true; 
    p.StartInfo.RedirectStandardOutput = true; 
    p.StartInfo.RedirectStandardError = true; 
    p.StartInfo.FileName = "c:\\ffmpeg.exe"; 
    p.StartInfo.Arguments = "-i - -y -vn -acodec libspeex -ac 1 -ar 16000 -f mp3 "; 

    StringBuilder output = new StringBuilder(); 

    p.OutputDataReceived += (sender, e) => 
    { 
     if (e.Data != null) 
     { 
     output.AppendLine(e.Data); 
     //eventually replace this with response.write code for a web request 
     } 
    }; 

    p.Start(); 
    StreamWriter ffmpegInput = p.StandardInput; 

    p.BeginOutputReadLine(); 

    using (Stream dataStream = response.GetResponseStream()) 
    { 
     int read = dataStream.Read(buffer, 0, block); 

     while (read > 0) 
     { 
      ffmpegInput.Write(buffer); 
      ffmpegInput.Flush(); 
      read = dataStream.Read(buffer, 0, block); 
     } 
    } 

    ffmpegInput.Close(); 

    var getErrors = p.StandardError.ReadToEnd(); 

그냥 궁금. 다른 가능한 C# 솔루션이있는 경우 열 수 있습니다.

감사

당신이 입력으로 WMA 파일을 출력으로 MP3 파일을 생성 할 것 같습니다

답변

1

. 또한 stdin/stdout을 통해이 작업을 수행하려는 것으로 보입니다. 난 그냥 명령 줄에서이 테스트가 작동하는 것 같다 :

ffmpeg -i - -f mp3 - <in.wma> out.mp3 

를이 명령 행에 몇 가지 문제가있다, 당신의 목표 인 경우 :

p.StartInfo.FileName = "c:\\ffmpeg.exe"; 
p.StartInfo.Arguments = "-i - -y -vn -acodec libspeex -ac 1 -ar 16000 -f mp3 "; 

"-f MP3"지정이 MP3 비트 스트림 컨테이너이지만 "-acodec libspeex"는 Speex 오디오를 지정합니다. 나는 너가 이것을 원하지 않는다고 확신한다. 그 옵션을 버리고 FFmpeg는 MP3 컨테이너 용 MP3 오디오를 사용하게됩니다. 또한 16000 Hz, 모노로 리샘플링 하시겠습니까? 이것이 "-ac 1 -ar 16000"옵션의 기능입니다.

마지막으로 한 가지 문제가 있습니다. 타겟을 제공하지 않습니다. stdout을 대상으로 지정하려면 "-f mp3 -"이 필요합니다.

0

같은 문제가있었습니다.

입력 사항으로 직접 URL을 포함 할 수 있습니다. 결과물로는 표준 출력을 나타내는 -을 사용합니다. 이 솔루션은 mp3 사이의 변환을위한 것이므로 비디오와 동일하게 작동하기를 바랍니다.

process.EnableRaisingEvents = true; 플래그를 반드시 포함해야합니다.


private void ConvertVideo(string srcURL) 
{ 
    string ffmpegURL = @"C:\ffmpeg.exe"; 
    DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\"); 

    ProcessStartInfo startInfo = new ProcessStartInfo(); 
    startInfo.FileName = ffmpegURL; 
    startInfo.Arguments = string.Format("-i \"{0}\" -ar 44100 -f mp3 -", srcURL); 
    startInfo.WorkingDirectory = directoryInfo.FullName; 
    startInfo.UseShellExecute = false; 
    startInfo.RedirectStandardOutput = true; 
    startInfo.RedirectStandardInput = true; 
    startInfo.RedirectStandardError = true; 
    startInfo.CreateNoWindow = false; 
    startInfo.WindowStyle = ProcessWindowStyle.Normal; 

    using (Process process = new Process()) 
    { 
     process.StartInfo = startInfo; 
     process.EnableRaisingEvents = true; 
     process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived); 
     process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived); 
     process.Exited += new EventHandler(process_Exited); 

     try 
     { 
      process.Start(); 
      process.BeginErrorReadLine(); 
      process.BeginOutputReadLine(); 
      process.WaitForExit(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      process.ErrorDataReceived -= new DataReceivedEventHandler(process_ErrorDataReceived); 
      process.OutputDataReceived -= new DataReceivedEventHandler(process_OutputDataReceived); 
      process.Exited -= new EventHandler(process_Exited); 

     } 
    } 
} 

void process_OutputDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    if (e.Data != null) 
    { 
     byte[] b = System.Text.Encoding.Unicode.GetBytes(e.Data); 
     // If you are in ASP.Net, you do a 
     // Response.OutputStream.Write(b) 
     // to send the converted stream as a response 
    } 
} 


void process_Exited(object sender, EventArgs e) 
{ 
    // Conversion is finished. 
    // In ASP.Net, do a Response.End() here. 
} 

PS :이 표준 출력 조각과 URL에서 '라이브'변환 할 수있어,하지만 'ASP'물건이 아직 시도하지 않은 의사 코드이다.

관련 문제