<div dir="ltr"><div>Hello all!</div><div>I am trying to replicate this ffmpeg command: ffmpeg -i <input> -vn -acodec copy <output>, using the libav APIs.</div><div><br></div><div>There seems to be a lack of updated examples and i really struggled to find any resources.</div><div>I was able to open the file both input and output, allocate format contexts for each, and copy general stream information, but not the packets themselves.</div><div><br></div><div>Here is the code that i gathered with some comments i have added,btw im running it on android :</div><div>```</div><div>void ffmpeg(</div><div> const char *in_filename,</div><div> const char *out_filename </div><div> ) {</div><div><br></div><div> //Libraries init</div><div> SSL_load_error_strings();</div><div> SSL_library_init();</div><div><br></div><div> av_register_all ();</div><div> avformat_network_init ();</div><div><br></div><div> //Set avlog to log on locat</div><div> av_log_set_callback(log_callback_android);</div><div><br></div><div> AVOutputFormat *ofmt = NULL;</div><div> AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;</div><div> AVPacket pkt;</div><div> int ret, i;</div><div><br></div><div> // Opening input file, and checking stream info</div><div> if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {</div><div> __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Could not open input file '%s'.Error %s", in_filename, av_err2str(ret));</div><div> goto end;</div><div> }</div><div> if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {</div><div> __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Failed to retrieve input stream information");</div><div> goto end;</div><div> }</div><div><br></div><div> //Print stream info</div><div> av_dump_format(ifmt_ctx, 0, in_filename, 0);</div><div><br></div><div><br></div><div> //Allocate new contex for output file</div><div> avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);</div><div> if (!ofmt_ctx) {</div><div> __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Could not create output context\n");</div><div> ret = AVERROR_UNKNOWN;</div><div> goto end;</div><div> }</div><div><br></div><div> //Setting output format to inhereted format</div><div> ofmt = ofmt_ctx->oformat;</div><div><br></div><div> // Iterate over all streams in input file</div><div> for (i = 0; i < ifmt_ctx->nb_streams; i++) {</div><div> //Stream input</div><div> AVStream *in_stream = ifmt_ctx->streams[i];</div><div><br></div><div> //Filter non audio streams</div><div> if(in_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO){</div><div> __android_log_print(ANDROID_LOG_INFO,APPNAME,"Audio stream found");</div><div><br></div><div> //in_stream->codec is deprecated, no idea how to get it otherway</div><div> //Making new stream in the output format context, based on the input stream</div><div> AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);</div><div> if (!out_stream) {</div><div> __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Failed allocating output stream\n");</div><div> ret = AVERROR_UNKNOWN;</div><div> goto end;</div><div> }</div><div><br></div><div> //Copying stream information to new contex </div><div> ret = avcodec_parameters_copy(out_stream->codecpar,in_stream->codecpar);</div><div> if (ret < 0) {</div><div> __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Failed to copy context from input to output stream codec context\n");</div><div> goto end;</div><div> }</div><div><br></div><div> }</div><div><br></div><div> }</div><div> //Print output format</div><div> av_dump_format(ofmt_ctx, 0, out_filename, 1);</div><div><br></div><div> //Open output file</div><div> if (!(ofmt->flags & AVFMT_NOFILE)) {</div><div> ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);</div><div> if (ret < 0) {</div><div> __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Could not open output file '%s'", out_filename);</div><div> goto end;</div><div> }</div><div> }</div><div><br></div><div> //Writing output header</div><div> ret = avformat_write_header(ofmt_ctx, NULL);</div><div> if (ret < 0) {</div><div> __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Error occurred when opening output file\n");</div><div> goto end;</div><div> }</div><div><br></div><div> //Mux packets - dont know what is happening here, assuming its copying the real data to the streams.</div><div> while (1) {</div><div> AVStream *in_stream, *out_stream;</div><div> ret = av_read_frame(ifmt_ctx, &pkt);</div><div> if (ret < 0)</div><div> break;</div><div><br></div><div> in_stream = ifmt_ctx->streams[pkt.stream_index];</div><div> out_stream = ofmt_ctx->streams[pkt.stream_index];</div><div> log_packet(ifmt_ctx, &pkt, "in");</div><div> //copy packet</div><div> pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_PASS_MINMAX);</div><div> pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_PASS_MINMAX);</div><div> pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);</div><div> pkt.pos = -1;</div><div> log_packet(ofmt_ctx, &pkt, "out");</div><div> ret = av_interleaved_write_frame(ofmt_ctx, &pkt);</div><div> if (ret < 0) {</div><div> __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Error muxing packet\n");</div><div> break;</div><div> }</div><div> av_packet_unref(&pkt);</div><div> }</div><div> av_write_trailer(ofmt_ctx);</div><div><br></div><div> end:</div><div> avformat_close_input(&ifmt_ctx);</div><div> //close output</div><div> if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))</div><div> avio_closep(&ofmt_ctx->pb);</div><div> //free output alloc</div><div> avformat_free_context(ofmt_ctx);</div><div> if (ret < 0 && ret != AVERROR_EOF) {</div><div> __android_log_print(ANDROID_LOG_ERROR, APPNAME, "Error occurred: %s\n", av_err2str(ret));</div><div><br></div><div> }</div><div> }</div><div>```</div><div>The muxing packets part,doesnt work, as it failed with signal error, after few iterations - probably error in this code, nothing to do with the libs themselves.</div><div><br></div><div>Here is an ouput of a run:</div><div>```</div><div>===Input Information===</div><div><br></div><div> Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '<input>':</div><div> Metadata:</div><div> major_brand : </div><div> isom</div><div> minor_version : </div><div> 512</div><div> compatible_brands: </div><div> isomiso2avc1mp41</div><div> encoder : </div><div> Lavf57.25.100</div><div> Duration: </div><div> 00:00:58.82</div><div> , start: </div><div> 0.000000</div><div> , bitrate: </div><div> 7369 kb/s</div><div> Stream #0:0</div><div> (eng)</div><div> Audio: aac (mp4a / 0x6134706D), 44100 Hz, 2 channels, 160 kb/s</div><div> (default)</div><div> Metadata:</div><div> handler_name : </div><div> SoundHandler</div><div> Stream #0:1</div><div> (eng)</div><div> Video: h264 (avc1 / 0x31637661), none, 640x640, 7213 kb/s</div><div><br></div><div> 30 fps, </div><div> 30 tbr, </div><div> 15360 tbn, </div><div> 15360 tbc</div><div> (default)</div><div> Metadata:</div><div> handler_name : </div><div> VideoHandler</div><div><br></div><div> ===Ouput Information===</div><div><br></div><div> Audio stream found</div><div> Output #0, adts, to 'out.aac':</div><div> Stream #0:0</div><div> Audio: aac (mp4a / 0x6134706D), 44100 Hz, 2 channels, 160 kb/s</div><div><br></div><div> ==Packet Logging==</div><div><br></div><div> in: pts:-2048 pts_time:-0.0464399 dts:-2048 dts_time:-0.0464399 duration:1024 duration_time:0.02322 stream_index:0</div><div> out: pts:-2048 pts_time:-0.0464399 dts:-2048 dts_time:-0.0464399 duration:1024 duration_time:0.02322 stream_index:0</div><div> in: pts:-1024 pts_time:-0.02322 dts:-1024 dts_time:-0.02322 duration:1024 duration_time:0.02322 stream_index:0</div><div> out: pts:-1024 pts_time:-0.02322 dts:-1024 dts_time:-0.02322 duration:1024 duration_time:0.02322 stream_index:0</div><div> in: pts:0 pts_time:0 dts:0 dts_time:0 duration:1024 duration_time:0.02322 stream_index:0</div><div> out: pts:0 pts_time:0 dts:0 dts_time:0 duration:1024 duration_time:0.02322 stream_index:0</div><div> in: pts:0 pts_time:0 dts:0 dts_time:0 duration:512 duration_time:0.0333333 stream_index:1</div><div><br></div><div>Error is </div><div>Signal: SIGSEGV (signal SIGSEGV: invalid address (fault address: 0x29))</div><div>```</div><div>I would like to get some help for the last part, how to correctly copy the packets, and also how to not use `in_stream->codec->codec`,since it is deprecated, but i couldn’t find a way to use with codecpar</div><div>I was able to retrieve the codec id, but couldn’t find a way to recive an AVcodec instance from it.</div><div><br></div><div>Sorry if this post is a duplicate, i wasnt sure i sent the last one correctly.</div><div>Here is a pastbin with the code blocks i included in case the markdown doesn't work properly : <a href="https://pastebin.com/dZgSZARM">https://pastebin.com/dZgSZARM</a></div><div><br></div><div>Cheers.</div><div>David Barishev.</div><div><br></div>
</div>