开发者

Reproduce PIPE functionality in IronPython

开发者 https://www.devze.com 2023-02-14 18:56 出处:网络
I am hoping some genious out there can help me out with this... I am using sox to merge and resample a group of WAV files, and pipe the output directly to the input of NeroAACEnc for encoding to AAC

I am hoping some genious out there can help me out with this...

I am using sox to merge and resample a group of WAV files, and pipe the output directly to the input of NeroAACEnc for encoding to AAC format.

I originally ran the process in a script, which included:

sox.exe d:\audio\1.wav d:\audio\2.wav d:\audio\3.wav -c 1 -r 22050 -t wav - | neroAacEnc.exe -q 0.5 -if - -of test.m4a

This worked as expected. The '-' in the comand line translates as 'Pipe/redirect input/output (stdin/stdout)' - So Sox pipes to stdout, and NeroAACEnc reads from stdin, the | joins them together.

I then migrated the whole solution to Python, and the equivalent command became:

from subprocess import call, Popen, PIPE
runwav = Popen(['sox.exe', 'd:\audio\1.wav', 'd:\audio\2.wav', 'd:\audio\3.wav', '-c', '1', '-r', '22050', '-t', 'wav', '-'], shell=False, stdout=PIPE)
runm4b = call(['neroAacEnc.exe', '-q', '0.5', '-if', '-', '-of', 'test.m4a'], shell=False, stdin=runwav.stdout)

This also worked like a charm, exactly as expected. Slightly more convoluted, but hey :)

Well now I have to move it to IronPython, and the Subprocess module isn't available (the partial implementation that is, doesn't have Popen/PIPE support - plus it seems silly to add a custom library when there is probably a native alternative). UPDATE The partial module DOES support Popen and PIPE, I mis-read it. It is possible to extract the specifics for the .net/IronPython solution from this module (see update below)

I should mention here, that I opted for IronPython over C#, because I am comfortable with Python now - however, there is a chance of moving it again later to C# native, and I 开发者_C百科am using IronPython to ease myself into it :) I have no C# or .net experience.

So far I have the following equivalent, that sets up the 2 processes:

from System.Diagnostics import Process
wav = Process()
wav.StartInfo.UseShellExecute = False
wav.StartInfo.RedirectStandardOutput = True
wav.StartInfo.FileName = 'sox.exe'
wav.StartInfo.Arguments = 'd:\audio\1.wav d:\audio\2.wav d:\audio\3.wav -c 1 -r 22050 -t wav -'       
wav.Start()

m4b = Process()
m4b.StartInfo.UseShellExecute = False
m4b.StartInfo.RedirectStandardInput = True
m4b.StartInfo.FileName = 'neroAacEnc.exe'
m4b.StartInfo.Arguments = '-q 0.5 -if - -of test.m4a'
m4b.Start()

I know that these 2 processes start (I can see Nero and Sox in the task manager) but what I can't figure out (for the life of me) is how to string the two output/input streams together, as with the previous two solutions. I have searched and searched, so I thought I'd ask!

If anyone knows either:

  1. How to join the two streams with the same net result as the Python and Commandline versions; or
  2. A better way to acheive what I am trying to do.

P.S. A code sample based off the above would be awesome :) or a specific code example of a similar process that I can easily translate.

[UPDATE]

After consideration, I resolved to handle my particular scenario as two seperate processes; outputting the merged WAVs to a file, then using that file as the input for the conversion.

However, it is possible to acheive what I wanted, using the subprocess module available here.


Windows has named pipes and you can use them from .NET applications like IronPython. It is usually straightforward to translate VB.NET examples like this one. You can often find managed libraries for things like this, for instance http://www.codeproject.com/KB/threads/dotnetnamedpipespart1.aspx

Here is another project to create a wrapper around the pipe api. http://omegacoder.com/?p=101

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号