简体   繁体   中英

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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