Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to make videos from images on Android?

how to make videos from images on Android?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 242 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Soviet-Ball
    wrote on last edited by
    #1

    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:" << 1024
    512;
    //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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        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 ?

        S Offline
        S Offline
        Soviet-Ball
        wrote on last edited by
        #3

        @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;
        }

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved