Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

c++ - 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);

question from:https://stackoverflow.com/questions/65924505/record-audio-data-to-an-existing-media-file-using-ffmpeg-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...