how to make videos from images on Android?
-
I tried to use ffmpeg to make a video from images, but it didn't work:
void makeVideo(QStringList srcList,QString desFile)
{
if(srcList.size()<=0){
return;
}
qDebug() << "........-> makeVedio";
av_register_all();
const char out_filename = desFile.toLocal8Bit().data();
qDebug() << "-------------------AVFormatContext oc 1";
AVFormatContext oc = NULL;
avformat_alloc_output_context2(&oc, NULL, NULL,out_filename);
if(!oc)
{
qDebug() << "-------------------AVFormatContext oc init failed";
return;
}
oc->oformat->video_codec = AV_CODEC_ID_MJPEG;
qDebug() << "-------------------AVStream st 2";
AVStream *st;
st = avformat_new_stream(oc, NULL);
if(!st)
{
av_free(oc);
qDebug() << "-------------------AVStream st init failed";
return;
}
qDebug() << "-------------------AVCodecContext c 3";
AVCodecContext *c;
av_dump_format(oc,0,out_filename,1);
c = st->codec;
// c->codec_id = oc->oformat->video_codec;
// c->codec_type = AVMEDIA_TYPE_VIDEO;
// c->bit_rate = 400000;
// c->width = 640;
// c->height = 360;
// c->time_base.den = 12;
// c->time_base.num =1;
// c->gop_size = 12;
// c->pix_fmt = AV_PIX_FMT_YUV420P;
// c->flags |= CODEC_FLAG_GLOBAL_HEADER;
// c->codec_tag = 0;
qDebug() << "-------------------AVCodec codec 4";
AVCodec codec;
codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!codec)
{
av_free(oc);
qDebug() << "-------------------AVCodec codec init failed";
return;
}
qDebug() << "-------------------avcodec_get_context_defaults3 5";
avcodec_get_context_defaults3(st->codec, codec);
//avcodec_get_context_defaults3(c,codec);
avcodec_open2(c,codec,NULL);
avcodec_parameters_from_context(st->codecpar,c);
AVCodecParameters codecpar = st->codecpar;
codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
codecpar->bit_rate = 400000;
codecpar->width = QImage(QString(srcList.at(1))).width();
codecpar->height = QImage(QString(srcList.at(1))).height();
//codecpar->format = 12;
st->time_base.den = 25;
st->time_base.num = 1;
st->codec->gop_size = 12;
st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
st->codec->codec_tag = 0;
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
{
qDebug() << "-------------------if (oc->oformat->flags & AVFMT_GLOBALHEADER) ";
//st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
st->codec->flags |= AV_CODEC_FLAG2_LOCAL_HEADER;
}
if (!(oc->oformat->flags & AVFMT_NOFILE))
{
int ret = avio_open(&oc->pb, out_filename, AVIO_FLAG_WRITE);
if (ret < 0) {
av_free(oc);
printf("Could not open output file '%s'", out_filename);
return;
}
qDebug() << "-------------------if (!(oc->oformat->flags & AVFMT_NOFILE)) ";
}
if (avformat_write_header(oc, NULL) < 0)
{
printf("Error occurred when opening output file\n");
return;
}
qDebug() << "------------------open file 6";
QFile file(srcList.at(0));
if (!file.open(QIODevice::ReadOnly))
{
printf("Error occurred when opening jpg file\n");
return;
}
QByteArray arr = file.readAll();
//char p = arr.data();
uint8_t mydata = (uint8_t)arr.data();
qDebug() << "-------------------arr.len:"<< arr.length() << " file.size:" << file.size() << " 1024 * 521:" << 1024512;
//QDataStream in(&file);
//in.readBytes(mydata,file.size());
//QTextStream in(&file);
AVPacket *pkt = av_packet_alloc();
qDebug() << "------------------av_interleaved_write_frame 7";
pkt->data = mydata;
pkt->size = file.size();
//fclose(file);
qDebug() << "------------------av_interleaved_write_frame 8";
if (av_interleaved_write_frame(oc, pkt) < 0)
{
printf("Error muxing packet\n");
return;
}
file.close();
qDebug() << "------------------free 9";
av_free_packet(pkt);
av_write_trailer(oc);
delete mydata;
if (oc && !(oc->oformat->flags & AVFMT_NOFILE))
avio_close(oc->pb);
avformat_free_context(oc);
}
how can I fix the function, or can I use ffmpeg command on Android? -
Hi,
What does your error checking tell you ?
The way you build out_filename is wrong.
Did you check that you are writing to a suitable folder ? -
@SGaist Well,I have found a way to make video with avilib on Android, thought it supports avi only.Anyway, thanks for your help.
static bool makeAVIVideo(QStringList pathlist,QString filepath,int frame,QString encoder = "MJPG")
{
if(pathlist.size() <= 0 || !hasFilePermission(filepath) || frame <= 0)
return false;
avi_t* avi = AVI_open_output_file(QByteArray(filepath.toLocal8Bit()).data());
AVI_set_video(avi, QImage(pathlist.at(0)).width(), QImage(pathlist.at(0)).height(), frame, QByteArray(encoder.toLocal8Bit()).data());
for(int i = 0;i < pathlist.size();i++)
{
QFile* file = new QFile(pathlist.at(i));
if(file->exists() && file->open(QIODevice::ReadOnly))
{
AVI_write_frame(avi,file->readAll().data(),file->size(),0);
file->close();
}
delete file;
}
AVI_close(avi);
if(QFile::exists(filepath))
return true;
else
return false;
}