⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lavaddwav.c

📁 Motion JPEG编解码器源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
          mjpeg_error("File: %s Reading video frame: %s",vp[iCurLav]->chFileName,lav_strerror());          lav_close(lav_out);          lav_close(lav_fd);          if( wav_fd >= 0 ) { close(wav_fd); }          free(abuff);          free(vbuff);          free_audio_memory(iWavFiles);          free_video_memory(iLavFiles);          exit(1);        }        frame_size = lav_frame_size(lav_fd,i);        /* write video frame */        res = lav_write_frame(lav_out, vbuff, frame_size, 1);        if( res < 0 )        {          mjpeg_error("Writing video frame: %s", lav_strerror());          lav_close(lav_out);          lav_close(lav_fd);          if( wav_fd >= 0 ) { close(wav_fd); }          free(abuff);          free(vbuff);          free_audio_memory(iWavFiles);          free_video_memory(iLavFiles);          exit(1);        }        ulOutputBytes += (unsigned long)frame_size;        /* Audio loop */        lRemBytes = absize;        iTotBytes = 0;        while( iCurWav < iWavFiles )        {          n = read(wav_fd, &abuff[iTotBytes], lRemBytes);          iTotBytes += n;          lRemBytes -= n;          if( n < 0 )          {            /* read error */            mjpeg_error("Error reading audio from file %s: %s", ap[iCurWav]->chFileName, lav_strerror());            lav_close(lav_out);            lav_close(lav_fd);            close(wav_fd);            free(abuff);            free(vbuff);            free_audio_memory(iWavFiles);            free_video_memory(iLavFiles);            exit(1);          }          if( iTotBytes < absize )          {            /* we need more data from next audio file if possible */            close(wav_fd);            wav_fd = -1;            iCurWav++;            if( iCurWav == iWavFiles )            {              /* no more audio files - make next if block write out data now */              absize = iTotBytes;            }          }          if( iTotBytes == absize )          {            /* got all the bytes we wanted - write them out */            res = lav_write_audio(lav_out, abuff, iTotBytes/ap[0]->audio_bps);            if( res < 0 )            {              mjpeg_error("Error writing audio: %s",lav_strerror());              lav_close(lav_out);              lav_close(lav_fd);              close(wav_fd);              free(abuff);              free(vbuff);              free_audio_memory(iWavFiles);              free_video_memory(iLavFiles);              exit(1);            }            ulOutputBytes += (unsigned long)iTotBytes;            /* we're done for this frame */            break;          } /* end of if( iTotBytes == absize ) */          /* we need more data from next audio file */          if( !open_wav_file(&wav_fd, ap[iCurWav]->chFileName, iCurWav, 0) )          {            mjpeg_error("Error opening %s", ap[iCurWav]->chFileName);            lav_close(lav_out);            lav_close(lav_fd);            free(abuff);            free(vbuff);            free_audio_memory(iWavFiles);            free_video_memory(iLavFiles);            exit(1);          }          total_audio_duration += ap[iCurWav]->time_length;        } /* end of while( iCurWav < iWavFiles ) */        /* Increment video frame counter */         i++;        /* Check for exceeding maximum output file size */        if( (ulOutputBytes >> 20) >= (unsigned long)param_maxfilesize )        {          if( imulti_out )          {            mjpeg_debug("  Starting new sequence: %d",iOutNum);            lav_close(lav_out);            ulOutputBytes = 0;            break;          }          /* No limit on QT files */          if( vp[0]->format != 113 )          {            mjpeg_error("Max file size reached use %%0xd in your output filename");            lav_close(lav_out);            lav_close(lav_fd);            if( wav_fd >= 0 ) { close(wav_fd); }            free(abuff);            free(vbuff);            free_audio_memory(iWavFiles);            free_video_memory(iLavFiles);            exit(0);          }        }      } /* end of while( i < vp[iCurLav]->video_frames ) */      /* Do we need to go on to the next video file? */      if( i == vp[iCurLav]->video_frames )      {        iCurLav++;        if( iCurLav < iLavFiles )        {          /* Open next video file */          lav_close(lav_fd);          if( !open_lav_file(&lav_fd, vp[iCurLav]->chFileName, iCurLav, 0) )          {            mjpeg_error("Error opening %s", vp[iCurLav]->chFileName);            if( lav_out != NULL ) { lav_close(lav_out); }            if( wav_fd >= 0 ) { close(wav_fd); }            free(abuff);            free(vbuff);            free_audio_memory(iWavFiles);            free_video_memory(iLavFiles);            exit(1);          }          total_video_duration += vp[iCurLav]->time_length;          /* reset video frame counter */          i = 0;        }        else        {          /* we've run out of video */          break;        }      } /* end of if( i == vp[iCurLav]->video_frames ) */      /* Do we need to create a new output file? */      if( ulOutputBytes == 0 )      {        break;      }    }/* end of while(1) (Loop reading and writing video and audio) */              } /* end of while( iCurLav < iLavFiles ) */  /* Copy remaining audio if required */  /* Outer loop */  while( iCurWav < iWavFiles )  {    if( ulOutputBytes == 0 )    {      /* build output filename */      sprintf(chOutFile, argv[optind+2], iOutNum++);            /* Create output file */      lav_out = lav_open_output_file(chOutFile,                                     vp[0]->format,                                     vp[0]->width,                                     vp[0]->height,                                     vp[0]->interlacing,                                     vp[0]->fps,                                     ap[0]->audio_bits,                                     ap[0]->audio_chans,                                     ap[0]->audio_rate);      if(!lav_out)      {        mjpeg_error("Error creating %s: %s", chOutFile, lav_strerror());        if( wav_fd >= 0 ) { close(wav_fd); }        free(abuff);        free(vbuff);        free_audio_memory(iWavFiles);        free_video_memory(iLavFiles);        exit(1);      }    } /* end of if( lav_out == NULL ) */    /* Audio loop */    while(1)    {      lRemBytes = absize;      iTotBytes = 0;      while( iCurWav < iWavFiles )      {        n = read(wav_fd, &abuff[iTotBytes], lRemBytes);        iTotBytes += n;        lRemBytes -= n;        if( n < 0 )        {          /* read error */          mjpeg_error("Error reading audio from file %s: %s", ap[iCurWav]->chFileName, lav_strerror());          lav_close(lav_out);          close(wav_fd);          free(abuff);          free(vbuff);          free_audio_memory(iWavFiles);          free_video_memory(iLavFiles);          exit(1);        }        if( iTotBytes < absize )        {          /* we need more data from next audio file if possible */          close(wav_fd);          wav_fd = -1;          iCurWav++;          if( iCurWav == iWavFiles )          {            /* no more audio files - make next if block write out data now */            absize = iTotBytes;          }        }        if( iTotBytes == absize )        {          /* got all the bytes we wanted - write them out */          res = lav_write_audio(lav_out, abuff, iTotBytes/ap[0]->audio_bps);          if( res < 0 )          {            mjpeg_error("Error writing audio: %s",lav_strerror());            lav_close(lav_out);            close(wav_fd);            free(abuff);            free(vbuff);            free_audio_memory(iWavFiles);            free_video_memory(iLavFiles);            exit(1);          }          ulOutputBytes += (unsigned long)iTotBytes;          /* we're done for this frame */          break;        } /* end of if( iTotBytes == absize ) */        /* we need more data from next audio file */        if( !open_wav_file(&wav_fd, ap[iCurWav]->chFileName, iCurWav, 0) )        {          mjpeg_error("Error opening %s", ap[iCurWav]->chFileName);          lav_close(lav_out);          free(abuff);          free(vbuff);          free_audio_memory(iWavFiles);          free_video_memory(iLavFiles);          exit(1);        }        total_audio_duration += ap[iCurWav]->time_length;      } /* end of while( iCurWav < iWavFiles ) */      /* Check for exceeding maximum output file size */      if( (ulOutputBytes >> 20) >= (unsigned long)param_maxfilesize )      {        if( imulti_out )        {          mjpeg_debug("  Starting new sequence: %d",iOutNum);          ulOutputBytes = 0;          break;        }        /* No limit on QT files */        if( vp[0]->format != 113 )        {          mjpeg_error("Max file size reached use %%0xd in your output filename");          lav_close(lav_out);          if( wav_fd >= 0 ) { close(wav_fd); }          free(abuff);          free(vbuff);          free_audio_memory(iWavFiles);          free_video_memory(iLavFiles);          exit(0);        }      }      if( iCurWav == iWavFiles )      {        /* no more audio files */        break;      }    } /* end of while(1) (Audio loop) */    lav_close(lav_out);    lav_out = NULL;  } /* end of while( iCurWav < iWavFiles ) */  /* Tidy up and exit */  if( lav_out != NULL )  {    lav_close(lav_out);  }  free(abuff);  free(vbuff);  free_audio_memory(iWavFiles);  free_video_memory(iLavFiles);  mjpeg_debug("   Total Video Duration:    %8.3f sec", total_video_duration);  mjpeg_debug("   Total Audio Duration:    %8.3f sec", total_audio_duration);  mjpeg_debug(" ");  /* Success */  return 0;}/********************************************************************/int open_lav_file(lav_file_t **lav_fd, char *chLavFile, int ientry, int iscan){  long i;  long max_frame_size = 0;  lav_file_t *lav_tmp;  *lav_fd = lav_open_input_file(chLavFile);  if(!*lav_fd)  {    mjpeg_error("Error opening %s", chLavFile);    return 0;  }  if( iscan )  {    strncpy(&vp[ientry]->chFileName[0], chLavFile, MAX_FILE_NAME);    vp[ientry]->video_frames = lav_video_frames(*lav_fd);    vp[ientry]->width        = lav_video_width(*lav_fd);    vp[ientry]->height       = lav_video_height(*lav_fd);    vp[ientry]->interlacing  = lav_video_interlacing(*lav_fd);    vp[ientry]->fps          = lav_frame_rate(*lav_fd);    if( vp[ientry]->fps <= 0.0 )    {      lav_close(*lav_fd);      mjpeg_error("Framerate illegal");      return 0;    }        vp[ientry]->time_length  = vp[ientry]->video_frames / vp[ientry]->fps;    lav_tmp = *lav_fd;    vp[ientry]->format       = lav_tmp->format;    /* find maximum frame size */    for(i=0; i<vp[ientry]->video_frames; i++)    {      if( lav_frame_size(*lav_fd, i) > max_frame_size )      {        max_frame_size = lav_frame_size(*lav_fd, i);      }    }    vp[ientry]->max_frame_size = max_frame_size;  }  /* success */  return 1;    }/********************************************************************/int open_wav_file(int *wav_fd, char *chWavFile, int ientry, int iscan){  int n;  uint32_t fmtlen;  uint32_t data[64];  off_t cur_off = 0;  off_t cur_end = 0;  *wav_fd = open(chWavFile, O_RDONLY);  if( *wav_fd < 0 )  {    mjpeg_error("Error opening WAV file %s :%s", chWavFile, strerror(errno));    return 0;  }  n = read(*wav_fd,(char*)data,20);  if( n != 20 )  {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -