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. Qt and ffmpeg, how to work with sound
Forum Updated to NodeBB v4.3 + New Features

Qt and ffmpeg, how to work with sound

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.1k Views
  • 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      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.

      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