[FFmpeg-user] Creating MP4 with G711 audio

Ferdi Scholten ferdi at sttc-nlp.nl
Thu Nov 21 20:06:26 EET 2024


On 21-11-2024 10:41, d.gordenin at ngslab.ru wrote:
> Hello.
>
> I'm trying to make a mp4 file from a video/audio stream. When audio is 
> AAC it works fine. But I cannot pack G711U audio.
>
> |av_register_all(); avcodec_register_all(); 
> av_log_set_callback(nullptr); av_format_context_ = 
> avformat_alloc_context(); if (!av_format_context_) { std::string 
> error_msg = "Failed to allocate avformat context"; 
> log_->Error(error_msg); throw std::runtime_error(error_msg.c_str()); } 
> av_format_context_->oformat = av_guess_format("mp4", filename.c_str(), 
> NULL); strcpy(av_format_context_->filename, filename.c_str()); 
> AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_PCM_MULAW); 
> av_format_context_->oformat->audio_codec = codec->id; audio_stream_ = 
> avformat_new_stream(av_format_context_, codec); if (audio_stream_ == 
> nullptr) { log_->Error("Can not create audiostream."); return false; } 
> AVCodecContext& codec_context = *audio_stream_->codec; if 
> (avcodec_copy_context(audio_stream_->codec, 
> av_format_context_->streams[1]->codec) != 0) { log_->Error("Failed to 
> Copy Context"); return false; } audio_stream_->time_base = { 1, 
> codec_context.sample_rate }; if (av_format_context_->oformat->flags & 
> AVFMT_GLOBALHEADER) codec_context.flags |= 
> AV_CODEC_FLAG_GLOBAL_HEADER; log_->Info("Init output {}", filename); 
> av_dump_format(av_format_context_, 0, filename.c_str(), 1); int ret = 
> avio_open(&av_format_context_->pb, filename.c_str(), AVIO_FLAG_WRITE); 
> if (ret < 0) { log_->Error("Can not open file for writing."); return 
> false; } ret = avformat_write_header(av_format_context_, NULL); if 
> (ret < 0) { log_->Error("Can not write header."); return false; } |
>
> Here avformat_write_header() returns -22. What's wrong here? Which 
> data should I supply to this function?
> _______________________________________________
The MP4 container only supports a few audio codecs namely these:
Advanced Audio Coding (AAC) 
<https://en.wikipedia.org/wiki/Advanced_Audio_Coding>, Audio Lossless 
Coding <https://en.wikipedia.org/wiki/Audio_Lossless_Coding> (ALS), 
Scalable Lossless Coding 
<https://en.wikipedia.org/wiki/Scalable_Lossless_Coding> (SLS), MP3 
<https://en.wikipedia.org/wiki/MP3>, MPEG-1 Audio Layer II 
<https://en.wikipedia.org/wiki/MPEG-1_Audio_Layer_II> (MP2), MPEG-1 
Audio Layer I <https://en.wikipedia.org/wiki/MPEG-1_Audio_Layer_I> 
(MP1), CELP <https://en.wikipedia.org/wiki/CELP>, HVXC 
<https://en.wikipedia.org/wiki/HVXC> (speech), TwinVQ 
<https://en.wikipedia.org/wiki/TwinVQ>, Text To Speech Interface (TTSI) 
and Structured Audio Orchestra Language 
<https://en.wikipedia.org/wiki/Structured_Audio_Orchestra_Language> 
(SAOL), Apple Lossless <https://en.wikipedia.org/wiki/Apple_Lossless>, 
Free Lossless Audio Codec <https://en.wikipedia.org/wiki/FLAC> (added in 
late 2018), and Opus <https://en.wikipedia.org/wiki/Opus_(audio_format)> 
(added in late 2018)

Use a different container like MKV if you want support for almost every 
audio codec available


More information about the ffmpeg-user mailing list