Gstreamer multifilesink wav files splitting

徘徊边缘 提交于 2019-12-10 10:48:50

问题


I have problem with recording streams using gstreamer. I have to write audio and video separately and cut in when signal arrived. I have correctly working video, but still have problems with wav files. Even simple pipeline in gst-launch don't work correctly. I have wave file and I am trying to split it using multifilesink:
gst-launch filesrc location=test.wav ! multifilesink location=test2%d.wav next-file=4 max-file-size=512000 But final wav files are corrupted while the same pipeline with ts files is working ok:
gst-launch-1.0 filesrc location=test.ts ! multifilesink location=test2%d.ts next-file=4 max-file-size=2000000


回答1:


multifilesink doesn't know anything about the data it splits up, so it won't take care of adding headers to each of the files it writes.

The reason why your .ts files work is because it was designed to be a streaming format where each separate packet will be treated independently. Therefore, one can just 'tune in' to the stream whenever one likes. The decoder will simply look for the next packet header it finds and start decoding there (for Details have a look at MPEG TS' wiki page.

The WAV file format however was designed as pure file (and not as a streaming) format. Therefore, there's only one header at the start of the file. When you split that file up into multiple files, these headers are missing (the file contains only raw PCM data then).

To work around that issue, you can...

  • manually copy the .wav header from the first file to all the other ones
  • use programs that support PCM files and either work directly with them or convert the files (you'll have to set the channel count, sample rate and bitrate manually when opening those files though).
  • use another, stream oriented file format like .mp3 which comes from the same family of codecs as .ts and also uses a separate 4-byte header for each frame (Keep in mind though that MP3 is a lossy file format).
    An example pipeline would be:

    gst-launch filesrc location=test.wav ! wavparse ! lame ! multifilesink location=test%d.mp3 next-file=4 max-file-size=100000
    



回答2:


If you're willing to use some scripting as well and split the task up into different gst-launch calls, I can offer you another possible way to solve your little problem:

The following script is a Linux bash script. You should be able to translate that to Windows batch script (or a C or python app if you want):

#!/bin/bash -e

# First write the buffer stream to .buff files (annotated using GStreamer's GDP format)
gst-launch -e filesrc location=test.wav ! wavparse ! gdppay ! multifilesink next-file=4 max-file-size=1000000 location=foo%05d.buff

# use the following instead for any other source (e.g. internet radio streams)
#gst-launch -e uridecodebin uri=http://url.to/stream ! gdppay ! multifilesink next-file=4 max-file-size=1000000 location=foo%05d.buff

# After we're done, convert each of the resulting files to proper .wav files with headers
for file in *.buff; do
    tgtFile="$(echo "$file"|sed 's/.buff$/.wav/')"

    gst-launch-0.10 filesrc "location=$file" ! gdpdepay ! wavenc ! filesink "location=$tgtFile"
done

# Uncomment the following line to remove the .buff files here, but to avoid accidentally 
# deleting stuff we haven't properly converted if something went wrong, I'm not gonna do that now.
#rm *.buff

Now to what the script does:

  • First we're gonna use multifilesink to create a set of .buff files, each under 1MB of size (gdppay will annotate each buffer with its caps; the -e flag of gst-launch will cause it to trigger an EOS if the process gets killed prematurely which is useful if you're reading and decoding an internet stream)
  • The second gst-launch invocation within the for loop takes one of the .buff files, parses the GDP headers using gdpdepay (and strips them), adds a WAV header and writes the result to a .wav file.

Hope this is a solution you can live with, because I doubt there's a way to do it with one single gst-launch run.



来源:https://stackoverflow.com/questions/25662392/gstreamer-multifilesink-wav-files-splitting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!