简体   繁体   English

使用 FFMPEG API 将音频数据录制到现有媒体文件

[英]Record audio data to an existing media file using FFMPEG API

My task is to record the received audio data in a media file.我的任务是将接收到的音频数据记录在媒体文件中。 I have no problem with this, everything works fine.我对此没有任何问题,一切正常。 But, when closing the audio file, I will no longer be able to re-open it and write the audio data to the end of the audio file.但是,当关闭音频文件时,我将不再能够重新打开它并将音频数据写入音频文件的末尾。 How do I solve this problem?我该如何解决这个问题? And in general, is it possible to write new data to the end of an existing media file?一般来说,是否可以将新数据写入现有媒体文件的末尾?

This is a piece of code where I record the trailer and close the media file:这是一段代码,我在其中录制预告片并关闭媒体文件:

// Writing the stream trailer to an output
// media file and free the file private data.
av_write_trailer(p_oFrmCtx);
avformat_close_input(&p_oFrmCtx);

As far as I know, opening an existing audio file and writing to it is not possible.据我所知,打开现有的音频文件并写入它是不可能的。 What you can do is write the incoming data to a new file and merge it with the previous one (the one at the end of which you wanted to write the data).您可以做的是将传入的数据写入一个新文件并将其与前一个文件(您想要写入数据的最后一个文件)合并。 You can use below command to achieve that.您可以使用以下命令来实现这一点。

ffmpeg -i "concat:file1.mp3|file2.mp3" -acodec copy output.mp3

If you have a list of files to be merged together, run the below command如果您有要合并在一起的文件列表,请运行以下命令

$ ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp3

Note the list should be of following format请注意,列表应采用以下格式

$ cat filelist.txt
file '/audio/001.mp3'
file '/audio/002.mp3'
file '/audio/003.mp3'
file '/audio/004.mp3'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM