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

📄 ffmpeg.c

📁 杜比AC-3编码解码器(参考程序)
💻 C
📖 第 1 页 / 共 2 页
字号:
    const char *str;
    int width, height;
} SizeEntry;

SizeEntry sizes[] = {
    { "sqcif", 128, 96 },
    { "qcif", 176, 144 },
    { "cif", 352, 288 },
    { "4cif", 704, 576 },
};
    
enum {
    OPT_AR=256,
    OPT_AB,
    OPT_AN,
    OPT_AC,
    OPT_VN,
    OPT_AD,
    OPT_HQ,
};

struct option long_options[] =
{
    { "ar", required_argument, NULL, OPT_AR },
    { "ab", required_argument, NULL, OPT_AB },
    { "an", no_argument, NULL, OPT_AN },
    { "ac", required_argument, NULL, OPT_AC },
    { "vn", no_argument, NULL, OPT_VN },
    { "ad", required_argument, NULL, OPT_AD },
    { "hq", no_argument, NULL, OPT_HQ },
};

enum {
    OUT_FILE,
    OUT_PIPE,
    OUT_UDP,
};


void help(void)
{
    AVFormat *f;

    printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000,2001 Gerard Lantau\n"
           "usage: ffmpeg [options] outfile [video_infile] [audio_infile]\n"
           "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio layer 2 encoder\n"
           "\n"
           "Main options are:\n"
           "\n"
           "-L           print the LICENSE\n"
           "-s size      set frame size                       [%dx%d]\n"
           "-f format    set encoding format                  [guessed]\n"
           "-r fps       set frame rate                       [%d]\n"
           "-b bitrate   set the total bitrate in kbit/s      [%d]\n"
           "-t time      set recording time in seconds        [%0.1f]\n"
           "-ar freq     set the audio sampling freq          [%d]\n"
           "-ab bitrate  set the audio bitrate in kbit/s      [%d]\n"
           "-ac channels set the number of audio channels     [%d]\n"
           "-an          disable audio recording              [%s]\n"
           "-vn          disable video recording              [%s]\n"
           "-hq          high quality mode (non real time)    [%s]\n"
           "\n"
           "Frame sizes abbreviations: sqcif qcif cif 4cif\n",
           frame_width, frame_height,
           frame_rate,
           bit_rate / 1000,
           recording_time,
           audio_freq,
           audio_bit_rate / 1000,
           audio_channels,
           audio_disable ? "yes" : "no",
           video_disable ? "yes" : "no",
           high_quality ? "yes" : "no");

    printf("Encoding video formats:");
    for(f = first_format; f != NULL; f = f->next)
        printf(" %s", f->name);
    printf("\n");

    printf("outfile can be a file name, - (pipe) or 'udp:host:port'\n"
           "\n"
           "Advanced options are:\n"
           "-d device    set video4linux device name          [%s]\n"
           "-ad device   set audio device name                [%s]\n"
           "-g gop_size  set the group of picture size        [%d]\n"
           "-i           use only intra frames                [%s]\n"
           "-c comment   set the comment string\n"
           "\n",
           v4l_device,
           audio_device,
           gop_size,
           intra_only ? "yes" : "no");
}

void licence(void)
{
    printf(
    "ffmpeg version " FFMPEG_VERSION "\n"
    "Copyright (c) 2000,2001 Gerard Lantau\n"
    "This program is free software; you can redistribute it and/or modify\n"
    "it under the terms of the GNU General Public License as published by\n"
    "the Free Software Foundation; either version 2 of the License, or\n"
    "(at your option) any later version.\n"
    "\n"
    "This program is distributed in the hope that it will be useful,\n"
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
    "GNU General Public License for more details.\n"
    "\n"
    "You should have received a copy of the GNU General Public License\n"
    "along with this program; if not, write to the Free Software\n"
    "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
    );
}

static unsigned char output_buffer[32768];

int main(int argc, char **argv)
{
    AVEncodeContext video_enc1, *video_enc = &video_enc1;
    AVEncodeContext audio_enc1, *audio_enc = &audio_enc1;
    UDPContext udp_ctx1, *udp_ctx = &udp_ctx1;
    AVFormatContext av_ctx1, *av_ctx = &av_ctx1;
    FILE *outfile;
    int i, c;
    char *filename;
    int output_type;
    int use_video, use_audio;

    register_avencoder(&ac3_encoder);
    register_avencoder(&mp2_encoder);
    register_avencoder(&mpeg1video_encoder);
    register_avencoder(&h263_encoder);
    register_avencoder(&rv10_encoder);
    register_avencoder(&mjpeg_encoder);
    register_avencoder(&divx_encoder);

    register_avformat(&mp2_format);
    register_avformat(&ac3_format);
    register_avformat(&mpeg1video_format);
    register_avformat(&h263_format);
    register_avformat(&mpeg_mux_format);
    register_avformat(&ra_format);
    register_avformat(&rm_format);
    register_avformat(&asf_format);
    register_avformat(&avi_format);
    register_avformat(&mpjpeg_format);
    register_avformat(&swf_format);

    file_format = NULL;
    
    for(;;) {
        c = getopt_long_only(argc, argv, "s:f:r:b:t:hd:g:ic:L", 
                             long_options, NULL);
        if (c == -1)
            break;
        switch(c) {
        case 'L':
            licence();
            exit(1);
        case 'h':
            help();
            exit(1);
        case 's':
            {
                int n = sizeof(sizes) / sizeof(SizeEntry);
                const char *p;

                for(i=0;i<n;i++) {
                    if (!strcmp(sizes[i].str, optarg)) {
                        frame_width = sizes[i].width;
                        frame_height = sizes[i].height;
                        break;
                    }
                }
                if (i == n) {
                    p = optarg;
                    frame_width = strtol(p, (char **)&p, 10);
                    if (*p)
                        p++;
                    frame_height = strtol(p, (char **)&p, 10);
                }
            }
            break;
        case 'f':
            {
                AVFormat *f;
                f = first_format;
                while (f != NULL && strcmp(f->name, optarg) != 0) f = f->next;
                if (f == NULL) {
                    fprintf(stderr, "Invalid format: %s\n", optarg);
                    exit(1);
                }
                file_format = f;
            }
            break;
        case 'r':
            {
                frame_rate = atoi(optarg);
            }
            break;
        case 'b':
            {
                bit_rate = atoi(optarg) * 1000;
            }
            break;
        case 't':
            {
                recording_time = atof(optarg);
                break;
            }
            /* audio specific */
        case OPT_AR:
            {
                audio_freq = atoi(optarg);
                break;
            }
        case OPT_AB:
            {
                audio_bit_rate = atoi(optarg) * 1000;
                break;
            }
        case OPT_AN:
            audio_disable = 1;
            break;
        case OPT_VN:
            video_disable = 1;
            break;
        case OPT_AC:
            {
                audio_channels = atoi(optarg);
                if (audio_channels != 1 && 
                    audio_channels != 2) {
                    fprintf(stderr, "Incorrect number of channels: %d\n", audio_channels);
                    exit(1);
                }
            }
            break;
        case OPT_HQ:
            high_quality = 1;
            break;
            /* advanced options */
        case 'd':
            v4l_device = optarg;
            break;
        case OPT_AD:
            audio_device = optarg;
            break;
        case 'g':
            gop_size = atoi(optarg);
            break;
        case 'i':
            intra_only = 1;
            break;
        case 'c':
            comment_string = optarg;
            break;
        default:
            exit(2);
        }
    }
    if (optind >= argc) {
        help();
        exit(1);
    }

    filename = argv[optind++];
    video_filename = NULL;
    audio_filename = NULL;

    /* auto detect format */
    if (file_format == NULL)
        file_format = guess_format(NULL, filename, NULL);

    if (file_format == NULL)
        file_format = &mpeg_mux_format;

    /* check parameters */
    if (frame_width <= 0 || frame_height <= 0) {
        fprintf(stderr, "Incorrect frame size\n");
        exit(1);
    }
    if ((frame_width % 16) != 0 || (frame_height % 16) != 0) {
        fprintf(stderr, "Frame size must be a multiple of 16\n");
        exit(1);
    }
    
    if (bit_rate < 5000 || bit_rate >= 10000000) {
        fprintf(stderr, "Invalid bit rate\n");
        exit(1);
    }

    if (frame_rate < 1 || frame_rate >= 60) {
        fprintf(stderr, "Invalid frame rate\n");
        exit(1);
    }

    nb_frames = (int)(recording_time * frame_rate);
    if (nb_frames < 1) {
        fprintf(stderr, "Invalid recording time\n");
        exit(1);
    }

    use_video = file_format->video_codec != CODEC_ID_NONE;
    use_audio = file_format->audio_codec != CODEC_ID_NONE;
    if (audio_disable) {
        use_audio = 0;
    }
    if (video_disable) {
        use_video = 0;
    }
        
    if (use_video == 0 && use_audio == 0) {
        fprintf(stderr, "No audio or video selected\n");
        exit(1);
    }

    fprintf(stderr, "Recording: %s, %0.1f seconds\n",
            file_format->name, 
            recording_time);

    /* open output media */

    if (strstart(filename, "udp:", NULL)) {
        output_type = OUT_UDP;
        outfile = NULL;
        memset(udp_ctx, 0, sizeof(*udp_ctx));
        if (udp_tx_open(udp_ctx, filename, 0) < 0) {
            fprintf(stderr, "Could not open UDP socket\n");
            exit(1);
        }
    } else if (!strcmp(filename, "-")) {
        output_type = OUT_PIPE;
        outfile = stdout;
    } else {
        output_type = OUT_FILE;
        outfile = fopen(filename, "w");
        if (!outfile) {
            perror(filename);
            exit(1);
        }
    }

    av_ctx->video_enc = NULL;
    av_ctx->audio_enc = NULL;

    if (output_type == OUT_UDP) {
        init_put_byte(&av_ctx->pb, output_buffer, sizeof(output_buffer),
                      udp_ctx, udp_write_data, NULL);
    } else {
        init_put_byte(&av_ctx->pb, output_buffer, sizeof(output_buffer),
                      outfile, raw_write_data, raw_seek);
    }

    if (use_video) {
        if (optind < argc) {
            video_filename = argv[optind++];
        }
        /* init mpeg video encoding context */
        memset(video_enc, 0, sizeof(*video_enc));
        video_enc->bit_rate = bit_rate;
        video_enc->rate = frame_rate; 

        video_enc->width = frame_width;
        video_enc->height = frame_height;
        if (!intra_only)
            video_enc->gop_size = gop_size;
        else
            video_enc->gop_size = 0;
        if (high_quality)
            video_enc->flags |= CODEC_FLAG_HQ;
        av_ctx->video_enc = video_enc;
        av_ctx->format = file_format;
    }

    if (use_audio) {
        if (optind < argc) {
            audio_filename = argv[optind++];
        }
        audio_enc->bit_rate = audio_bit_rate;
        audio_enc->rate = audio_freq;
        audio_enc->channels = audio_channels;
        av_ctx->audio_enc = audio_enc;
    }
    av_ctx->format = file_format;
    av_ctx->is_streamed = 0;

    av_encode(av_ctx, video_filename, audio_filename);

    /* close output media */

    switch(output_type) {
    case OUT_FILE:
        fclose(outfile);
        break;
    case OUT_PIPE:
        break;
    case OUT_UDP:
        udp_tx_close(udp_ctx);
        break;
    }
    fprintf(stderr, "\n");
    
    return 0;
}
    

⌨️ 快捷键说明

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