Пример #1
0
int main(int argc, char **argv)
{
  int    had_video_name = FALSE;
  int    had_audio_name = FALSE;
  int    had_output_name = FALSE;
  char  *video_name = NULL;
  char  *audio_name = NULL;
  char  *output_name = NULL;
  int    err = 0;
  ES_p   video_es = NULL;
  access_unit_context_p h264_video_context = NULL;
  avs_context_p avs_video_context = NULL;
  int    audio_file = -1;
  TS_writer_p output1 = NULL;
//  TS_writer_p output2 = NULL;
  int    quiet = FALSE;
  int    verbose = FALSE;
  int    debugging = FALSE;
  int    audio_samples_per_frame = ADTS_SAMPLES_PER_FRAME;
  int    audio_sample_rate = CD_RATE;
  int    video_frame_rate = DEFAULT_VIDEO_FRAME_RATE;
  int    audio_type = AUDIO_ADTS;
  int    video_type = VIDEO_H264;
  int    pat_pmt_freq = 0;
  int    ii = 1;

#if TEST_PTS_DTS
  test_pts();
  return 0;
#endif

  if (argc < 2)
  {
    print_usage();
    return 0;
  }

  while (ii < argc)
  {
    if (argv[ii][0] == '-')
    {
      if (!strcmp("--help",argv[ii]) || !strcmp("-help",argv[ii]) ||
          !strcmp("-h",argv[ii]))
      {
        print_usage();
        return 0;
      }
      else if (!strcmp("-err",argv[ii]))
      {
        CHECKARG("esmerge",ii);
        if (!strcmp(argv[ii+1],"stderr"))
          redirect_output_stderr();
        else if (!strcmp(argv[ii+1],"stdout"))
          redirect_output_stdout();
        else
        {
          fprint_err("### esmerge: "
                     "Unrecognised option '%s' to -err (not 'stdout' or"
                     " 'stderr')\n",argv[ii+1]);
          return 1;
        }
        ii++;
      }
      else if (!strcmp("-verbose",argv[ii]) || !strcmp("-v",argv[ii]))
      {
        verbose = TRUE;
      }
      else if (!strcmp("-quiet",argv[ii]) || !strcmp("-q",argv[ii]))
      {
        quiet = TRUE;
      }
      else if (!strcmp("-x",argv[ii]))
      {
        debugging = TRUE;
        quiet = FALSE;
      }
      else if (!strcmp("-rate",argv[ii]))
      {
        CHECKARG("esmerge",ii);
        err = int_value("esmerge",argv[ii],argv[ii+1],TRUE,10,&audio_sample_rate);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-cd",argv[ii]))
      {
        audio_sample_rate = CD_RATE;
      }
      else if (!strcmp("-dat",argv[ii]))
      {
        audio_sample_rate = DAT_RATE;
      }
      else if (!strcmp("-vidrate",argv[ii]))
      {
        CHECKARG("esmerge",ii);
        err = int_value("esmerge",argv[ii],argv[ii+1],TRUE,10,&video_frame_rate);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-adts",argv[ii]))
      {
        audio_type = AUDIO_ADTS;
      }
      else if (!strcmp("-l2",argv[ii]))
      {
        audio_type = AUDIO_L2;
      }
      else if (!strcmp("-ac3", argv[ii]))
      {
        audio_type = AUDIO_AC3;
      }
      else if (!strcmp("-h264",argv[ii]))
      {
        video_type = VIDEO_H264;
      }
      else if (!strcmp("-mp2adts", argv[ii]))
      {
        audio_type = AUDIO_ADTS_MPEG2;
      }
      else if (!strcmp("-mp4adts", argv[ii]))
      {
        audio_type = AUDIO_ADTS_MPEG4;
      }
      else if (!strcmp("-avs",argv[ii]))
      {
        video_type = VIDEO_AVS;
      }
      else if (!strcmp("-patpmtfreq", argv[ii]))
      {
        CHECKARG("esmerge",ii);
        err = int_value("esmerge", argv[ii], argv[ii+1], TRUE, 10, &pat_pmt_freq);
        if (err) { return 1; }
        ++ii;
      }
      else
      {
        fprint_err("### esmerge: "
                   "Unrecognised command line switch '%s'\n",argv[ii]);
        return 1;
      }
    }
    else
    {
      if (!had_video_name)
      {
        video_name = argv[ii];
        had_video_name = TRUE;
      }
      else if (!had_audio_name)
      {
        audio_name = argv[ii];
        printf("got audio name 123 %s\n",audio_name);
        had_audio_name = TRUE;
      }
      else if (!had_output_name)
      {
        output_name = argv[ii];
        had_output_name = TRUE;
      }
      else
      {
        fprint_err("### esmerge: Unexpected '%s'\n",argv[ii]);
        return 1;
      }
    }
    ii++;
  }

  if (!had_video_name)
  {
    print_err("### esmerge: No video input file specified\n");
    return 1;
  }
 /*
  if (!had_audio_name)
  {
    print_err("### esmerge: No audio input file specified\n");
    return 1;
  }*/
  if (!had_output_name)
  {
    print_err("### esmerge: No output file specified\n");
    return 1;
  }
  char output_name1[255];
  char output_name2[255];
  strcpy(output_name1, output_name);
  strcpy(output_name2, output_name);
  int num1=atoi(audio_name);
  char fileSerial1[20];
  char fileSerial2[20];
  sprintf(fileSerial1,"%d.ts", num1);
  sprintf(fileSerial2,"%d.te", num1);
  strcat(output_name1,fileSerial1);
  strcat(output_name2,fileSerial2);
  uint32_t timeElapsed=0;

  err = open_elementary_stream(video_name,&video_es);
  if (err)
  {
    print_err("### esmerge: "
              "Problem starting to read video as ES - abandoning reading\n");
    return 1;
  }

  if (video_type == VIDEO_H264)
  {
    err = build_access_unit_context(video_es,&h264_video_context);
    if (err)
    {
      print_err("### esmerge: "
                "Problem starting to read video as H.264 - abandoning reading\n");
      close_elementary_stream(&video_es);
      return 1;
    }
  }
  else if (video_type == VIDEO_AVS)
  {
    err = build_avs_context(video_es,&avs_video_context);
    if (err)
    {
      print_err("### esmerge: "
                "Problem starting to read video as H.264 - abandoning reading\n");
      close_elementary_stream(&video_es);
      return 1;
    }
  }
  else
  {
    print_err("### esmerge: Unknown video type\n");
    return 1;
  }

  /*
  audio_file = open_binary_file(audio_name,FALSE);
  if (audio_file == -1)
  {
    print_err("### esmerge: "
              "Problem opening audio file - abandoning reading\n");
    close_elementary_stream(&video_es);
    free_access_unit_context(&h264_video_context);
    free_avs_context(&avs_video_context);
    return 1;
  }*/

  err = tswrite_open(TS_W_FILE,output_name1,NULL,0,quiet,&output1);
  //err = tswrite_open(TS_W_FILE,output_name2,NULL,0,quiet,&output2);
  if (err)
  {
    fprint_err("### esmerge: "
               "Problem opening output file %s - abandoning reading\n",
               output_name);
    close_elementary_stream(&video_es);
    close_file(audio_file);
    free_access_unit_context(&h264_video_context);
    free_avs_context(&avs_video_context);
    return 1;
  }

/*  switch (audio_type)
  {
  case AUDIO_ADTS:
    audio_samples_per_frame = ADTS_SAMPLES_PER_FRAME;
    break;
  case AUDIO_L2:
    audio_samples_per_frame = L2_SAMPLES_PER_FRAME;
    break;
  case AUDIO_AC3:
    audio_samples_per_frame = AC3_SAMPLES_PER_FRAME;
    break;
  default:              // hmm - or we could give up...
    audio_samples_per_frame = ADTS_SAMPLES_PER_FRAME;
    break;
  }
*/
  if (!quiet)
  {
    fprint_msg("Reading video from %s\n",video_name);
    fprint_msg("Reading audio from %s (as %s)\n",audio_name,AUDIO_STR(audio_type));
    fprint_msg("Writing output to  %s\n",output_name);
    fprint_msg("Audio sample rate: %dHz (%.2fKHz)\n",audio_sample_rate,
               audio_sample_rate/1000.0);
    fprint_msg("Audio samples per frame: %d\n",audio_samples_per_frame);
    fprint_msg("Video frame rate: %dHz\n",video_frame_rate);
  }


  if (video_type == VIDEO_H264)
    err = merge_with_h264(h264_video_context,audio_file,output1,//output2,
                          audio_type,
                          audio_samples_per_frame,audio_sample_rate,
                          video_frame_rate,
                          pat_pmt_freq,
                          quiet,verbose,debugging,num1, &timeElapsed);
  else if (video_type == VIDEO_AVS)
    printf("disable");
    /*err = merge_with_avs(avs_video_context,audio_file,output,
                         audio_type,
                         audio_samples_per_frame,audio_sample_rate,
                         video_frame_rate,
                         pat_pmt_freq,
                         quiet,verbose,debugging);*/
  else
  {
    print_err("### esmerge: Unknown video type\n");
    return 1;
  }
  if (err)
  {
    print_err("### esmerge: Error merging video and audio streams\n");
    close_elementary_stream(&video_es);
    close_file(audio_file);
    free_access_unit_context(&h264_video_context);
    free_avs_context(&avs_video_context);
    (void) tswrite_close(output1,quiet);
    //(void) tswrite_close(output2,quiet);
    return 1;
  }

  FILE *pFileLen = fopen (output_name2,"w");
  fprintf(pFileLen, "%.2f", timeElapsed/100.0);
  fclose(pFileLen);

  close_elementary_stream(&video_es);
  close_file(audio_file);
  free_access_unit_context(&h264_video_context);
  free_avs_context(&avs_video_context);
  err = tswrite_close(output1,quiet);
  //err = tswrite_close(output2,quiet);
  if (err)
  {
    fprint_err("### esmerge: Error closing output %s\n",output_name);
    return 1;
  }
  return 0;
}
Пример #2
0
int main(int argc, char **argv)
{
  int    had_video_name = FALSE;
  int    had_audio_name = FALSE;
  int    had_output_name = FALSE;
  char  *video_name = NULL;
  char  *audio_name = NULL;
  char  *output_name = NULL;
  int    err = 0;
  ES_p   video_es = NULL;
  access_unit_context_p h264_video_context = NULL;
  avs_context_p avs_video_context = NULL;
  int    audio_file = -1;
  TS_writer_p output = NULL;
  int    quiet = FALSE;
  int    verbose = FALSE;
  int    debugging = FALSE;
  int    audio_samples_per_frame = ADTS_SAMPLES_PER_FRAME;
  int    audio_sample_rate = CD_RATE;
  int    video_frame_rate = DEFAULT_VIDEO_FRAME_RATE;
  int    audio_type = AUDIO_ADTS;
  int    video_type = VIDEO_H264;
  int    pat_pmt_freq = 0;
  int    ii = 1;

#if TEST_PTS_DTS
  test_pts();
  return 0;
#endif

  if (argc < 2)
  {
    print_usage();
    return 0;
  }

  while (ii < argc)
  {
    if (argv[ii][0] == '-')
    {
      if (!strcmp("--help",argv[ii]) || !strcmp("-help",argv[ii]) ||
          !strcmp("-h",argv[ii]))
      {
        print_usage();
        return 0;
      }
      else if (!strcmp("-verbose",argv[ii]) || !strcmp("-v",argv[ii]))
      {
        verbose = TRUE;
      }
      else if (!strcmp("-quiet",argv[ii]) || !strcmp("-q",argv[ii]))
      {
        quiet = TRUE;
      }
      else if (!strcmp("-x",argv[ii]))
      {
        debugging = TRUE;
        quiet = FALSE;
      }
      else if (!strcmp("-rate",argv[ii]))
      {
        CHECKARG("esmerge",ii);
        err = int_value("esmerge",argv[ii],argv[ii+1],TRUE,10,&audio_sample_rate);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-cd",argv[ii]))
      {
        audio_sample_rate = CD_RATE;
      }
      else if (!strcmp("-dat",argv[ii]))
      {
        audio_sample_rate = DAT_RATE;
      }
      else if (!strcmp("-vidrate",argv[ii]))
      {
        CHECKARG("esmerge",ii);
        err = int_value("esmerge",argv[ii],argv[ii+1],TRUE,10,&video_frame_rate);
        if (err) return 1;
        ii++;
      }
      else if (!strcmp("-adts",argv[ii]))
      {
        audio_type = AUDIO_ADTS;
      }
      else if (!strcmp("-l2",argv[ii]))
      {
        audio_type = AUDIO_L2;
      }
      else if (!strcmp("-ac3", argv[ii]))
      {
        audio_type = AUDIO_AC3;
      }
      else if (!strcmp("-h264",argv[ii]))
      {
        video_type = VIDEO_H264;
      }
      else if (!strcmp("-mp2adts", argv[ii]))
      {
        audio_type = AUDIO_ADTS_MPEG2;
      }
      else if (!strcmp("-mp4adts", argv[ii]))
      {
        audio_type = AUDIO_ADTS_MPEG4;
      }
      else if (!strcmp("-avs",argv[ii]))
      {
        video_type = VIDEO_AVS;
      }
      else if (!strcmp("-patpmtfreq", argv[ii]))
        {
          err = int_value("patpmtfreq", argv[ii], argv[ii+1], TRUE, 10, &pat_pmt_freq);
          if (err) { return 1; }
          ++ii;
        }
      else
      {
        fprintf(stderr,"### esmerge: "
                "Unrecognised command line switch '%s'\n",argv[ii]);
        return 1;
      }
    }
    else
    {
      if (!had_video_name)
      {
        video_name = argv[ii];
        had_video_name = TRUE;
      }
      else if (!had_audio_name)
      {
        audio_name = argv[ii];
        had_audio_name = TRUE;
      }
      else if (!had_output_name)
      {
        output_name = argv[ii];
        had_output_name = TRUE;
      }
      else
      {
        fprintf(stderr,"### esmerge: Unexpected '%s'\n",argv[ii]);
        return 1;
      }
    }
    ii++;
  }

  if (!had_video_name)
  {
    fprintf(stderr,"### esmerge: No video input file specified\n");
    return 1;
  }
  if (!had_audio_name)
  {
    fprintf(stderr,"### esmerge: No audio input file specified\n");
    return 1;
  }
  if (!had_output_name)
  {
    fprintf(stderr,"### esmerge: No output file specified\n");
    return 1;
  }

  err = open_elementary_stream(video_name,&video_es);
  if (err)
  {
    fprintf(stderr,"### esmerge: "
            "Problem starting to read video as ES - abandoning reading\n");
    return 1;
  }

  if (video_type == VIDEO_H264)
  {
    err = build_access_unit_context(video_es,&h264_video_context);
    if (err)
    {
      fprintf(stderr,"### esmerge: "
              "Problem starting to read video as H.264 - abandoning reading\n");
      close_elementary_stream(&video_es);
      return 1;
    }
  }
  else if (video_type == VIDEO_AVS)
  {
    err = build_avs_context(video_es,&avs_video_context);
    if (err)
    {
      fprintf(stderr,"### esmerge: "
              "Problem starting to read video as H.264 - abandoning reading\n");
      close_elementary_stream(&video_es);
      return 1;
    }
  }
  else
  {
    fprintf(stderr,"### esmerge: Unknown video type\n");
    return 1;
  }

  audio_file = open_binary_file(audio_name,FALSE);
  if (audio_file == -1)
  {
    fprintf(stderr,"### esmerge: "
            "Problem opening audio file - abandoning reading\n");
    close_elementary_stream(&video_es);
    free_access_unit_context(&h264_video_context);
    free_avs_context(&avs_video_context);
    return 1;
  }

  err = tswrite_open(TS_W_FILE,output_name,NULL,0,quiet,&output);
  if (err)
  {
    fprintf(stderr,"### esmerge: "
            "Problem opening output file %s - abandoning reading\n",
            output_name);
    close_elementary_stream(&video_es);
    close_file(audio_file);
    free_access_unit_context(&h264_video_context);
    free_avs_context(&avs_video_context);
    return 1;
  }

  switch (audio_type)
  {
  case AUDIO_ADTS:
    audio_samples_per_frame = ADTS_SAMPLES_PER_FRAME;
    break;
  case AUDIO_L2:
    audio_samples_per_frame = L2_SAMPLES_PER_FRAME;
    break;
  case AUDIO_AC3:
    audio_samples_per_frame = AC3_SAMPLES_PER_FRAME;
    break;
  default:              // hmm - or we could give up...
    audio_samples_per_frame = ADTS_SAMPLES_PER_FRAME;
    break;
  }

  if (!quiet)
  {
    printf("Reading video from %s\n",video_name);
    printf("Reading audio from %s (as %s)\n",audio_name,AUDIO_STR(audio_type));
    printf("Writing output to  %s\n",output_name);
    printf("Audio sample rate: %dHz (%.2fKHz)\n",audio_sample_rate,
           audio_sample_rate/1000.0);
    printf("Audio samples per frame: %d\n",audio_samples_per_frame);
    printf("Video frame rate: %dHz\n",video_frame_rate);
  }


  if (video_type == VIDEO_H264)
    err = merge_with_h264(h264_video_context,audio_file,output,
                          audio_type,
                          audio_samples_per_frame,audio_sample_rate,
                          video_frame_rate,
                          pat_pmt_freq,
                          quiet,verbose,debugging);
  else if (video_type == VIDEO_AVS)
    err = merge_with_avs(avs_video_context,audio_file,output,
                         audio_type,
                         audio_samples_per_frame,audio_sample_rate,
                         video_frame_rate,
                         pat_pmt_freq,
                         quiet,verbose,debugging);
  else
  {
    fprintf(stderr,"### esmerge: Unknown video type\n");
    return 1;
  }
  if (err)
  {
    fprintf(stderr,"### esmerge: Error merging video and audio streams\n");
    close_elementary_stream(&video_es);
    close_file(audio_file);
    free_access_unit_context(&h264_video_context);
    free_avs_context(&avs_video_context);
    (void) tswrite_close(output,quiet);
    return 1;
  }

  close_elementary_stream(&video_es);
  close_file(audio_file);
  free_access_unit_context(&h264_video_context);
  free_avs_context(&avs_video_context);
  err = tswrite_close(output,quiet);
  if (err)
  {
    fprintf(stderr,"### esmerge: Error closing output %s\n",output_name);
    return 1;
  }
  return 0;
}