Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt and ffmpeg, how to work with sound



  • Hi!
    I'm reading a video file using ffmpeg.
    This is how I get audio data:

        static struct SwsContext *img_convert_ctx;
        int videoStream, i, numBytes, audioStream;
        int ret, got_picture;
    
        avformat_network_init();   //初始化FFmpeg网络模块
        av_register_all();  //初始化FFMPEG  调用了这个才能正常适用编码器和解码器
    
    
        AVFormatContext *pFormatCtx = NULL;
        //Allocate an AVFormatContext.
        pFormatCtx = avformat_alloc_context();
    
        // Open video file
        if(avformat_open_input(&pFormatCtx, "./Wildlife.wmv", 0, 0) != 0)
            qDebug()<<"Couldn't open file";
    
        if(avformat_find_stream_info(pFormatCtx, NULL) < 0)
         qDebug()<<"Couldn't find stream information";
    
        av_dump_format(pFormatCtx, 0, "./Wildlife.wmv", 0);//information abaut file
    
        //vide0
        AVCodecContext *pCodecCtxOrig = NULL;
        AVCodecContext *pCodecCtx = NULL;
        //audio
        AVCodecContext *aCodecCtxOrig;
        AVCodecContext *aCodecCtx;
    
        // Find the first video and audio stream
        videoStream = -1;
        audioStream = -1;
    
        for(i=0; i < pFormatCtx->nb_streams; i++) {
            if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO
                    &&
                    videoStream < 0) {
                videoStream=i;
            }
            if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
                    audioStream < 0) {
                audioStream=i;
            }
        }
        if(videoStream==-1)
            qDebug()<<"Didn't find a video stream";
        if(audioStream==-1)
            qDebug()<<"Didn't find a audio stream";
    
        // Get a pointer to the codec context for the video stream
        pCodecCtx=pFormatCtx->streams[videoStream]->codec;
        //audio
        aCodecCtxOrig=pFormatCtx->streams[audioStream]->codec;
        //aCodecCtx=pFormatCtx->streams[audioStream]->codec;
    
        //audio
        AVCodec *aCodec;
    
        aCodec = avcodec_find_decoder(aCodecCtxOrig->codec_id);
        if(!aCodec) {
         qDebug()<<"Unsupported codec!";
        }
        // Copy context
        aCodecCtx = avcodec_alloc_context3(aCodec);
        if(avcodec_copy_context(aCodecCtx, aCodecCtxOrig) != 0) {
         qDebug()<<"Couldn't copy codec contextt";
        }
        /* set up SDL Audio here */
    
        avcodec_open2(aCodecCtx, aCodec, NULL);
    

    Using STL it is suggested to play the sound like this:

    wanted_spec.freq = aCodecCtx->sample_rate;
    wanted_spec.format = AUDIO_S16SYS;
    wanted_spec.channels = aCodecCtx->channels;
    wanted_spec.silence = 0;
    wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
    wanted_spec.callback = audio_callback;
    wanted_spec.userdata = aCodecCtx;
    
    if(SDL_OpenAudio(&wanted_spec, &spec) < 0) {
     fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
     return -1;
    }
    

    I started doing this:

        QAudioRecorder audioRecorder;    
        QAudioEncoderSettings audioSettings;
        audioSettings.setCodec("audio/amr");
        audioSettings.setQuality(QMultimedia::HighQuality);
        audioRecorder.setEncodingSettings(audioSettings);
    

    How do I play audio using Qt ?



  • QAudioRecorder is for recording, not playing.
    You should look at QAudioOutput.
    Or you can use SDL as the Tutorial suggest.
    But the synchronization of audio and video playing is very complicated.
    I'm recently working on video playing with ffmpeg in Qt, too.
    Eventually instead of writing all the playing process myself, I choose to use QtAV.
    You can check on that.


Log in to reply