Closed ViswanathB84 closed 6 years ago
You should show relevant source code of your app to get help from anyone.
Thank you very much for your support. I am getting below error in open_output_file method at encoder = avcodec_find_encoder(dec_ctx->codec_id); line and it returns null value.
XPC connection interrupted Input #0, mpeg, from '/Users/viswanath/Library/Developer/CoreSimulator/Devices/A71B16D3-86A9-4403-A6B1-9A8D23B842C0/data/Containers/Bundle/Application/73503E38-AF99-4539-8453-FBB5C8B28F46/DLGDemo.app/iOSQuickstartMVVM.mpg': Duration: 00:05:15.69, start: 0.508278, bitrate: 982 kb/s Stream #0:0[0x1e0]: Video: mpeg1video, yuv420p(tv), 1280x720 [SAR 1:1 DAR 16:9], 104857 kb/s, 30 fps, 30 tbr, 90k tbn, 30 tbc Stream #0:1[0x1c0]: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s Necessary encoder not found Error occurred: Invalid data found when processing input
`
static AVFormatContext ifmt_ctx; static AVFormatContext ofmt_ctx; typedef struct FilteringContext { AVFilterContext buffersink_ctx; AVFilterContext buffersrc_ctx; AVFilterGraph filter_graph; } FilteringContext; static FilteringContext filter_ctx; typedef struct StreamContext { AVCodecContext dec_ctx; AVCodecContext enc_ctx; } StreamContext; static StreamContext *stream_ctx;
@implementation FFMpegVideoConversion
static int open_input_file(const char filename) { int ret; unsigned int i; ifmt_ctx = NULL; if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) { av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n"); return ret; } if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) { av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n"); return ret; } stream_ctx = av_mallocz_array(ifmt_ctx->nb_streams, sizeof(stream_ctx)); if (!stream_ctx) return AVERROR(ENOMEM); for (i = 0; i < ifmt_ctx->nb_streams; i++) { AVStream stream = ifmt_ctx->streams[i]; AVCodec dec = avcodec_find_decoder(stream->codecpar->codec_id); AVCodecContext codec_ctx; if (!dec) { av_log(NULL, AV_LOG_ERROR, "Failed to find decoder for stream #%u\n", i); return AVERROR_DECODER_NOT_FOUND; } codec_ctx = avcodec_alloc_context3(dec); if (!codec_ctx) { av_log(NULL, AV_LOG_ERROR, "Failed to allocate the decoder context for stream #%u\n", i); return AVERROR(ENOMEM); } ret = avcodec_parameters_to_context(codec_ctx, stream->codecpar); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Failed to copy decoder parameters to input decoder context " "for stream #%u\n", i); return ret; } / Reencode video & audio and remux subtitles etc. / if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) { if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) codec_ctx->framerate = av_guess_frame_rate(ifmt_ctx, stream, NULL); / Open decoder / ret = avcodec_open2(codec_ctx, dec, NULL); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i); return ret; } } stream_ctx[i].dec_ctx = codec_ctx; } av_dump_format(ifmt_ctx, 0, filename, 0); return 0; } static int open_output_file(const char filename) { AVStream out_stream; AVStream in_stream; AVCodecContext dec_ctx, enc_ctx; AVCodec encoder; int ret; unsigned int i; ofmt_ctx = NULL; avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename); if (!ofmt_ctx) { av_log(NULL, AV_LOG_ERROR, "Could not create output context\n"); return AVERROR_UNKNOWN; } for (i = 0; i < ifmt_ctx->nb_streams; i++) { out_stream = avformat_new_stream(ofmt_ctx, NULL); if (!out_stream) { av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n"); return AVERROR_UNKNOWN; } in_stream = ifmt_ctx->streams[i]; dec_ctx = stream_ctx[i].dec_ctx; if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) { / in this example, we choose transcoding to same codec / encoder = avcodec_find_encoder(dec_ctx->codec_id); if (!encoder) { av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n"); return AVERROR_INVALIDDATA; } enc_ctx = avcodec_alloc_context3(encoder); if (!enc_ctx) { av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n"); return AVERROR(ENOMEM); } / In this example, we transcode to same properties (picture size,
rewrite retcode to 0 to show it as normal procedure completion */ if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) ret = 0; av_frame_free(&filt_frame); break; } filt_frame->pict_type = AV_PICTURE_TYPE_NONE; ret = encode_write_frame(filt_frame, stream_index, NULL); if (ret < 0) break; } return ret; } static int flush_encoder(unsigned int stream_index) { int ret; int got_frame; if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities & AV_CODEC_CAP_DELAY)) return 0; while (1) { av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index); ret = encode_write_frame(NULL, stream_index, &got_frame); if (ret < 0) break; if (!got_frame) return 0; } return ret; }
if ((ret = open_input_file([inputPath UTF8String])) < 0) goto end; if ((ret = open_output_file([outputPath UTF8String])) < 0) goto end; if ((ret = init_filters()) < 0) goto end; / read all packets / while (1) { if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0) break; stream_index = packet.stream_index; type = ifmt_ctx->streams[packet.stream_index]->codecpar->codec_type; av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n", stream_index); if (filter_ctx[stream_index].filter_graph) { av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n"); frame = av_frame_alloc(); if (!frame) { ret = AVERROR(ENOMEM); break; } av_packet_rescale_ts(&packet, ifmt_ctx->streams[stream_index]->time_base, stream_ctx[stream_index].dec_ctx->time_base); dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 : avcodec_decode_audio4; ret = dec_func(stream_ctx[stream_index].dec_ctx, frame, &got_frame, &packet); if (ret < 0) { av_frame_free(&frame); av_log(NULL, AV_LOG_ERROR, "Decoding failed\n"); break; } if (got_frame) { frame->pts = frame->best_effort_timestamp; ret = filter_encode_write_frame(frame, stream_index); av_frame_free(&frame); if (ret < 0) goto end; } else { av_frame_free(&frame); } } else { / remux this frame without reencoding / av_packet_rescale_ts(&packet, ifmt_ctx->streams[stream_index]->time_base, ofmt_ctx->streams[stream_index]->time_base); ret = av_interleaved_write_frame(ofmt_ctx, &packet); if (ret < 0) goto end; } av_packet_unref(&packet); } / flush filters and encoders / for (i = 0; i < ifmt_ctx->nb_streams; i++) { / flush filter / if (!filter_ctx[i].filter_graph) continue; ret = filter_encode_write_frame(NULL, i); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n"); goto end; } / flush encoder / ret = flush_encoder(i); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n"); goto end; } } av_write_trailer(ofmt_ctx); end: av_packet_unref(&packet); av_frame_free(&frame); for (i = 0; i < ifmt_ctx->nb_streams; i++) { avcodec_free_context(&stream_ctx[i].dec_ctx); if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && stream_ctx[i].enc_ctx) avcodec_free_context(&stream_ctx[i].enc_ctx); if (filter_ctx && filter_ctx[i].filter_graph) avfilter_graph_free(&filter_ctx[i].filter_graph); } av_free(filter_ctx); av_free(stream_ctx); avformat_close_input(&ifmt_ctx); if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) avio_closep(&ofmt_ctx->pb); avformat_free_context(ofmt_ctx); if (ret < 0) av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
if (ret < 0) { NSLog(@"* video conversion Fail to convert"); oncomplete(false); }else { NSLog(@"*** video conversion Success"); oncomplete(true); }
}
@end `
You should log failing codec info and verify it is available. You may need to rebuild FFmpeg with more external libraries and/or configure
options.
I rebuilded FFmpeg libraries and below libraries using for conversion but Still it is failing. libavcodec.a libavdevice.a libavfilter.a libavformat.a libavresample.a libavutil.a libfdk-aac.a libpostproc.a libswresample.a libswscale.a libx264.a Would you please suggest which external libraries or configure I need to use it for conversion.
Thank you very much for developing and supporting this great FFMPEG libaray .
I am an iOS developer. I used FFMPEG library for video conversion i.e mpg format to mp4 format. I am getting below error in debug console.
Input #0, mpeg, from '/Users/Library/Developer/CoreSimulator/Devices/A71B16D3-86A9-4403-A6B1-9A8D23B842C0/data/Containers/Bundle/Application/FFA13844-61BD-4A81-9A2F-CACDB6BC6ADA/DLGDemo.app/iOSQuickstartMVVM.mpg': Duration: 00:05:15.69, start: 0.508278, bitrate: 982 kb/s Stream #0:0[0x1e0]: Video: mpeg1video, yuv420p(tv), 1280x720 [SAR 1:1 DAR 16:9], 104857 kb/s, 30 fps, 30 tbr, 90k tbn, 30 tbc Stream #0:1[0x1c0]: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s [h264_videotoolbox @ 0x7ff34a018200] Error setting max bitrate property: -12902 Cannot open video encoder for stream #0 Error occurred: Generic error in an external library.
Would you please help on this how to resolve this issue. Thank you very much for your support.