How to open file name contain special characters in FFmpeg? - c++

my project process the video by FFmpeg libarary, I test the my code with file which the file name contains special characters(like '()+,-.09#A[]^_`a{}~®,Ѐ,ऄ, ఆ, Ⴀ,ᐁ,ᣀ,ᴀ,₠,⓪,⭙,ⶀ,㈀,啊.mp4'), but it failed to open the video stream at avformat_open_input().
I test it via ffmpeg.exe with command line, it can open the file successfully.
This is my code snippet:
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
AVFrame *pFrameRGB = NULL;
unsigned int i, videoStream;
AVCodec *pCodec;
AVFrame *pFrame;
AVPacket packet;
int frameFinished;
int numBytes;
uint8_t *buffer;
AVPixelFormat ImgFmt = AV_PIX_FMT_YUV420P;
// Register all formats and codecs
av_register_all();
// Open video file
if (avformat_open_input(&pFormatCtx, inputFilePath, NULL, NULL) != 0)
{
printf("Couldn't open file.");
return -1;
}
it occur error at the avformat_open_input(), I think it failed because of the Unicode file name string
Anybody can help me?
Thanks

Related

auxDIBImageLoad returns null

Hi i am doing a project with opengl
What I'm stuck with is that the method 'auxDIBImageLoad' always returns null even if the file exists.
My code is below:
AUX_RGBImageRec *LoadBMPFile(char *filename)
{
FILE *hFile = NULL;
if (!filename) return NULL;
hFile = fopen(filename, "r");
if (hFile)
{
printf("file opened\n");
fclose(hFile);
if (!auxDIBImageLoad(filename)) {
printf("but file is null\n");
}
return auxDIBImageLoad( filename );
}
printf("file open failed\n");
return NULL;
}
Then the log is below:
file opened
but file is null
load bmp failed
I already searched for fixing it so that I already set to Multibyte char set.
But it doesn't work after setting that.
Please help me.

Cannot wrie header FFMPEG

I'm working on a Project with FFMPEg, but somehow i cannot creae any media file. I've set the Parameters, codec etc. but when i call avformat_write_header the app just stops working.
Here's my code:
AVOutputFormat *fmt;
AVCodecContext *codecctx;
AVFormatContext *fmtctx;
fmt=av_guess_format(NULL,filename,NULL);
AVCodec *codec = avcodec_find_encoder(fmt->video_codec);
codecctx = avcodec_alloc_context3(codec);
codecctx->codec_id = fmt->video_codec;
codecctx->codec_type = AVMEDIA_TYPE_VIDEO;
codecctx->gop_size = 12;
codecctx->bit_rate = WIDTH*HEIGHT*4;
codecctx->width = WIDTH;
codecctx->height = HEIGHT;
codecctx->time_base.den = 2;
codecctx->time_base.num =1;
//codecctx->time_base =(AVRational){1,FPS};
codecctx->max_b_frames=1;
codecctx->pix_fmt= STREAM_PIX_FMT;
fmtctx = avformat_alloc_context();
fmtctx->oformat = fmt;
fmtctx->video_codec_id = fmt->video_codec;
avcodec_open2(codecctx, codec, NULL);
AVStream *VideoStream = avformat_new_stream(fmtctx,codec);
avformat_write_header(fmtctx,NULL);
The main API functions for muxing are avformat_write_header() for writing the file header, av_write_frame() / av_interleaved_write_frame() for writing the packets and av_write_trailer() for finalizing the file.
check this link
http://ffmpeg.org/doxygen/trunk/group__lavf__encoding.html#details

avcodec_open2 error -542398533 : "Generic error in an external library"

I am encountering an error when trying to open the codec with avcodec_open2(). I have tried the same code without any problems if I specify avi instead of h264 in the av_guess_format() function.
I don't know what to make of it. Has anyone else encountered a similar problem?
The library that I'm using is ffmpeg-20160219-git-98a0053-win32-dev. I would really really appreciate if you could help me out of this confusion.
This is my console output:
Video encoding
[libx264 # 01383460] broken ffmpeg default settings detected
[libx264 # 01383460] use an encoding preset (e.g. -vpre medium)
[libx264 # 01383460] preset usage: -vpre -vpre
[libx264 # 01383460] speed presets are listed in x264 --help
[libx264 # 01383460] profile is optional; x264 defaults to high
Cannot open video codec, -542398533
This is the code that I'm working with:
// Video encoding sample
AVCodec *codec = NULL;
AVCodecContext *codecCtx= NULL;
AVFormatContext *pFormatCtx = NULL;
AVOutputFormat *pOutFormat = NULL;
AVStream * pVideoStream = NULL;;
AVFrame *picture = NULL;;
int i, x, y, ret;
printf("Video encoding\n");
// Register all formats and codecs
av_register_all();
// guess format from file extension
pOutFormat = av_guess_format("h264", NULL, NULL);
if (NULL==pOutFormat){
cerr << "Could not guess output format" << endl;
return -1;
}
// allocate context
pFormatCtx = avformat_alloc_context();
pFormatCtx->oformat = pOutFormat;
memcpy(pFormatCtx->filename,filename,
min(strlen(filename), sizeof(pFormatCtx->filename)));
// Add stream to pFormatCtx
pVideoStream = avformat_new_stream(pFormatCtx, 0);
if (!pVideoStream)
{
printf("Cannot add new video stream\n");
return -1;
}
// Set stream's codec context
codecCtx = pVideoStream->codec;
codecCtx->codec_id = (AVCodecID)pOutFormat->video_codec;
codecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
codecCtx->frame_number = 0;
// Put sample parameters.
codecCtx->bit_rate = 2000000;
// Resolution must be a multiple of two.
codecCtx->width = 320;
codecCtx->height = 240;
codecCtx->time_base.den = 10;
codecCtx->time_base.num = 1;
pVideoStream->time_base.den = 10;
pVideoStream->time_base.num = 1;
codecCtx->gop_size = 12; // emit one intra frame every twelve frames at most
codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
if (codecCtx->codec_id == AV_CODEC_ID_H264)
{
// Just for testing, we also add B frames
codecCtx->mb_decision = 2;
}
// Some formats want stream headers to be separate.
if(pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)
{
codecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
if(codecCtx->codec_id == AV_CODEC_ID_H264)
av_opt_set(codecCtx->priv_data, "preset", "slow", 0);
// Open the codec.
codec = avcodec_find_encoder(codecCtx->codec_id);
if (codec == NULL) {
fprintf(stderr, "Codec not found\n");
return -1;
}
ret = avcodec_open2(codecCtx, codec, NULL); // returns -542398533 here
if (ret < 0)
{
printf("Cannot open video codec, %d\n",ret);
return -1;
}
Your problem is this line:
codecCtx = pVideoStream->codec;
This AVCodecContext was allocated using global defaults, which x264 rejects because they are not optimal. Instead, use avcodec_alloc_context3 to allocate it, which will set x264-specific defaults. At the end of your encoding, don't forget to avcodec_free_context the returned pointer.
you should pass codec param to avformat_new_stream
codec = avcodec_find_encoder(codecCtx->codec_id);
pVideoStream = avformat_new_stream(pFormatCtx, codec);

RTP server for audio stream

I'm in the mid of my project where I'm trying to send the audio data over RTP using ffmpeg in VC++.
but I'm unable to do that.
My start up code is:
setRTPOutputAddr("127.0.0.1");
setRTPOutputPort(9985);
av_register_all();
avformat_network_init();
formatCtx= avformat_alloc_context();
//av_dump_format(formatCtx,0,"rtp",1);
avio_open_dyn_buf(&ioctx);
avio_open(&formatCtx->pb, "rtp", AVIO_FLAG_WRITE);
//av_dump_format(formatCtx,0,"rtp",1);
formatCtx->pb = ioctx;
rtpCtx = avformat_alloc_context();
rtpCtx->oformat = av_guess_format("rtp",0,0);
AVStream *fakeVideo = 0;
fakeVideo = avformat_new_stream(rtpCtx,0);
avcodec_get_context_defaults3(fakeVideo->codec, NULL);
fakeVideo->codec->codec_id = AV_CODEC_ID_MPEG2TS;
rtpCtx->audio_codec_id = AV_CODEC_ID_NONE;
rtpCtx->video_codec_id = AV_CODEC_ID_MPEG2TS;
// avformat_write_header(rtpCtx,0);
char *url = new char[1024];
sprintf(url,"rtp://%s:%d",rtpOutputAddr,rtpOutputPort);
printf("will output to url:%s\n",url);
avio_open(&rtpCtx->pb,url,AVIO_FLAG_WRITE);
avformat_write_header(rtpCtx, NULL);
delete url;
if (avio_open(&formatCtx->pb, "rtp", AVIO_FLAG_WRITE) < 0)
{
fprintf(stderr, "Could not open '%s'\n", formatCtx->filename);
return -1;
}
else
{
printf("succeed \n");
}
Error Here: avformat_write_header(formatCtx, NULL); //program Crashes
Do anyone having any idea where I'm doing wrong?
Thanks!

Problems converting .flv to mp3 using FFmpeg SDK

I'm using the FFmpeg SDK to programmatically convert videos to mp3.
I read the audio frames of the video this way:
while(av_read_frame(pFromCtx, &pkt) >= 0)
{
if(pkt.stream_index == audioStreamIndex)
{
avcodec_get_frame_defaults(frame);
got_frame = 0;
ret = avcodec_decode_audio4(pCodecCtx, frame, &got_frame, &pkt);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error decoding audio frame.\n");
continue;
}
if(got_frame)
{
// Write the decoded audio frame
write_audio_frame(pToCtx, pToCtx->streams[pToCtx->nb_streams-1], frame);
}
}
av_free_packet(&pkt);
}
Decoding the audio from the input video file works fine. The problem occurs when I try to encode a mp3 frame:
static void write_audio_frame(AVFormatContext *oc, AVStream *st, AVFrame *frame)
{
AVCodecContext *enc = st->codec;
AVPacket pkt;
int got_packet = 0;
int ret = 0;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
ret = avcodec_encode_audio2(enc, &pkt, frame, &got_packet);
if (ret < 0) {
// PROBLEM
fprintf(stderr, "Error encoding audio frame. \n");
exit(1);
}
}
I get the following console output:
[libmp3lame] inadequate AVFrame plane padding
The only happens with .flv files, the code works fine for .mp4 files. Any clue what the error message means?
Thanks
The source code containing the error message is here: http://ffmpeg.org/doxygen/trunk/libmp3lame_8c-source.html. The relevant source says:
if (frame->linesize[0] < 4 * FFALIGN(frame->nb_samples, 8)) {
av_log(avctx, AV_LOG_ERROR, "inadequate AVFrame plane padding\n");
return AVERROR(EINVAL);
}
FFALIGN is defined as
#define FFALIGN (x,a)(((x)+(a)-1)&~((a)-1))