2 * Basic user interface for ffmpeg system
3 * Copyright (c) 2000 Gerard Lantau
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <netinet/in.h>
22 #include <linux/videodev.h>
23 #include <linux/soundcard.h>
26 #include <sys/ioctl.h>
33 #include "mpegvideo.h"
35 static AVFormat *file_format;
36 static int frame_width = 160;
37 static int frame_height = 128;
38 static int frame_rate = 25;
39 static int bit_rate = 200000;
40 static int video_disable = 0;
42 static const char *video_filename, *audio_filename;
43 static float recording_time = 10.0;
45 static int gop_size = 12;
46 static int intra_only = 0;
47 static int audio_freq = 44100;
48 static int audio_bit_rate = 64000;
49 static int audio_disable = 0;
50 static int audio_channels = 1;
52 static long long time_start;
55 static int file_read_picture(AVEncodeContext *s,
57 int width, int height,
63 static UINT8 *pict[3];
65 pict[0] = malloc(width * height);
66 pict[1] = malloc(width * height / 4);
67 pict[2] = malloc(width * height / 4);
75 sprintf(buf, "%s%d.Y", video_filename, picture_number);
81 fread(picture[0], 1, width * height, f);
84 sprintf(buf, "%s%d.U", video_filename, picture_number);
90 fread(picture[1], 1, width * height / 4, f);
93 sprintf(buf, "%s%d.V", video_filename, picture_number);
99 fread(picture[2], 1, width * height / 4, f);
104 static void display_stats(AVEncodeContext *video_ctx,
105 AVEncodeContext *audio_ctx,
106 int batch_mode, int the_end)
109 ((video_ctx->frame_number % video_ctx->rate) == 0 ||
114 ti = (float)video_ctx->frame_number / video_ctx->rate;
116 ti = (gettime() - time_start) / 1000000.0;
122 "frame=%5d size=%8dkB time=%0.1f fps=%4.1f bitrate=%6.1fkbits/s q=%2d\r",
123 video_ctx->frame_number,
124 data_out_size / 1024,
126 video_ctx->frame_number / ti,
127 (data_out_size * 8 / ti / 1000),
128 ((MpegEncContext *)video_ctx->priv_data)->qscale);
130 fprintf(stderr,"\n");
135 if (the_end && batch_mode && audio_ctx) {
136 duration = (gettime() - ti) / 1000000.0;
139 factor = (float)nb_samples / s->sample_rate / duration;
141 fprintf(stderr, "%0.1f seconds compressed in %0.1f seconds (speed factor: %0.1f)\n",
142 (float)nb_samples / s->sample_rate,
149 void raw_write_data(void *opaque,
150 unsigned char *buf, int size)
152 FILE *outfile = opaque;
153 fwrite(buf, 1, size, outfile);
154 data_out_size += size;
157 int raw_seek(void *opaque, long long offset, int whence)
159 FILE *outfile = opaque;
160 fseek(outfile, offset, whence);
164 static void av_encode(AVFormatContext *ctx,
165 const char *video_filename,
166 const char *audio_filename)
168 UINT8 audio_buffer[4096];
169 UINT8 video_buffer[128*1024];
177 AVEncodeContext *audio_enc, *video_enc;
178 int frame_size, frame_bytes;
179 AVEncoder *audio_encoder, *video_encoder;
183 audio_enc = ctx->audio_enc;
191 if (audio_filename ||
197 if (!audio_filename) {
198 fprintf(stderr, "Must give audio input file\n");
201 infile = fopen(audio_filename, "r");
203 fprintf(stderr, "Could not open '%s'\n", audio_filename);
208 audio_fd = audio_open(audio_enc->rate, audio_enc->channels);
210 fprintf(stderr, "Could not open audio device\n");
215 audio_encoder = avencoder_find(ctx->format->audio_codec);
216 if (avencoder_open(audio_enc, audio_encoder) < 0) {
217 fprintf(stderr, "Audio encoder: incorrect audio frequency or bitrate\n");
220 avencoder_string(buf, sizeof(buf), audio_enc);
221 fprintf(stderr, " %s\n", buf);
223 frame_size = audio_enc->frame_size;
225 frame_bytes = frame_size * 2 * audio_enc->channels;
226 samples = malloc(frame_bytes);
230 video_enc = ctx->video_enc;
233 if (!video_filename) {
234 fprintf(stderr, "Must give video input file\n");
238 ret = v4l_init(video_enc->rate, video_enc->width, video_enc->height);
240 fprintf(stderr,"Could not init video 4 linux capture\n");
245 video_encoder = avencoder_find(ctx->format->video_codec);
246 if (avencoder_open(video_enc, video_encoder) < 0) {
247 fprintf(stderr, "Error while initializing video codec\n");
251 avencoder_string(buf, sizeof(buf), video_enc);
252 fprintf(stderr, " %s\n", buf);
255 ctx->format->write_header(ctx);
256 time_start = gettime();
259 /* read & compression audio frames */
263 ret = read(audio_fd, samples, frame_bytes);
264 if (ret != frame_bytes)
266 ret = avencoder_encode(audio_enc,
267 audio_buffer, sizeof(audio_buffer), samples);
268 ctx->format->write_audio_frame(ctx, audio_buffer, ret);
272 sample_count += audio_enc->rate / video_enc->rate;
274 sample_count += frame_size;
275 while (sample_count > frame_size) {
276 if (fread(samples, 1, frame_bytes, infile) == 0)
279 ret = avencoder_encode(audio_enc,
280 audio_buffer, sizeof(audio_buffer), samples);
281 ctx->format->write_audio_frame(ctx, audio_buffer, ret);
283 sample_count -= frame_size;
289 /* read video image */
291 ret = file_read_picture (video_enc, picture,
292 video_enc->width, video_enc->height,
293 video_enc->frame_number);
295 ret = v4l_read_picture (picture,
296 video_enc->width, video_enc->height,
297 video_enc->frame_number);
301 ret = avencoder_encode(video_enc, video_buffer, sizeof(video_buffer), picture);
302 ctx->format->write_video_picture(ctx, video_buffer, ret);
305 display_stats(video_enc, NULL, batch_mode, 0);
306 if (video_enc && video_enc->frame_number >= nb_frames)
310 display_stats(video_enc, NULL, batch_mode, 1);
313 avencoder_close(video_enc);
316 avencoder_close(audio_enc);
318 ctx->format->write_trailer(ctx);
332 SizeEntry sizes[] = {
333 { "sqcif", 128, 96 },
334 { "qcif", 176, 144 },
336 { "4cif", 704, 576 },
347 struct option long_options[] =
349 { "ar", required_argument, NULL, OPT_AR },
350 { "ab", required_argument, NULL, OPT_AB },
351 { "an", no_argument, NULL, OPT_AN },
352 { "ac", required_argument, NULL, OPT_AC },
353 { "vn", no_argument, NULL, OPT_VN },
367 printf("ffmpeg version 1.0, Copyright (c) 2000 Gerard Lantau\n"
368 "usage: ffmpeg [options] outfile [video_infile] [audio_infile]\n"
369 "Hyper fast MPEG1 video/H263/RV and AC3/MPEG audio layer 2 encoder\n"
371 "Main options are:\n"
373 "-L print the LICENSE\n"
374 "-s size set frame size [%dx%d]\n"
375 "-f format set encoding format [%s]\n"
376 "-r fps set frame rate [%d]\n"
377 "-b bitrate set the total bitrate in kbit/s [%d]\n"
378 "-t time set recording time in seconds [%0.1f]\n"
379 "-ar freq set the audio sampling freq [%d]\n"
380 "-ab bitrate set the audio bitrate in kbit/s [%d]\n"
381 "-ac channels set the number of audio channels [%d]\n"
382 "-an disable audio recording [%s]\n"
383 "-vn disable video recording [%s]\n"
385 "Frame sizes abbreviations: sqcif qcif cif 4cif\n",
386 frame_width, frame_height,
392 audio_bit_rate / 1000,
394 audio_disable ? "yes" : "no",
395 video_disable ? "yes" : "no");
397 printf("Encoding video formats:");
398 for(f = first_format; f != NULL; f = f->next)
399 printf(" %s", f->name);
402 printf("outfile can be a file name, - (pipe) or 'udp:host:port'\n"
404 "Advanced options are:\n"
405 "-d device set video4linux device name\n"
406 "-g gop_size set the group of picture size [%d]\n"
407 "-i use only intra frames [%s]\n"
408 "-c comment set the comment string\n"
411 intra_only ? "yes" : "no");
417 "ffmpeg version 1.0\n"
418 "Copyright (c) 2000 Gerard Lantau\n"
419 "This program is free software; you can redistribute it and/or modify\n"
420 "it under the terms of the GNU General Public License as published by\n"
421 "the Free Software Foundation; either version 2 of the License, or\n"
422 "(at your option) any later version.\n"
424 "This program is distributed in the hope that it will be useful,\n"
425 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
426 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
427 "GNU General Public License for more details.\n"
429 "You should have received a copy of the GNU General Public License\n"
430 "along with this program; if not, write to the Free Software\n"
431 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
435 static unsigned char output_buffer[32768];
437 int main(int argc, char **argv)
439 AVEncodeContext video_enc1, *video_enc = &video_enc1;
440 AVEncodeContext audio_enc1, *audio_enc = &audio_enc1;
441 UDPContext udp_ctx1, *udp_ctx = &udp_ctx1;
442 AVFormatContext av_ctx1, *av_ctx = &av_ctx1;
447 int use_video, use_audio;
449 register_avencoder(&ac3_encoder);
450 register_avencoder(&mp2_encoder);
451 register_avencoder(&mpeg1video_encoder);
452 register_avencoder(&h263_encoder);
453 register_avencoder(&rv10_encoder);
454 register_avencoder(&mjpeg_encoder);
456 register_avformat(&mp2_format);
457 register_avformat(&ac3_format);
458 register_avformat(&mpeg1video_format);
459 register_avformat(&h263_format);
460 register_avformat(&mpeg_mux_format);
461 register_avformat(&ra_format);
462 register_avformat(&rm_format);
463 register_avformat(&asf_format);
464 register_avformat(&mpjpeg_format);
465 register_avformat(&swf_format);
470 c = getopt_long_only(argc, argv, "s:f:r:b:t:hd:g:ic:L",
483 int n = sizeof(sizes) / sizeof(SizeEntry);
487 if (!strcmp(sizes[i].str, optarg)) {
488 frame_width = sizes[i].width;
489 frame_height = sizes[i].height;
495 frame_width = strtol(p, (char **)&p, 10);
498 frame_height = strtol(p, (char **)&p, 10);
506 while (f != NULL && strcmp(f->name, optarg) != 0) f = f->next;
508 fprintf(stderr, "Invalid format: %s\n", optarg);
516 frame_rate = atoi(optarg);
521 bit_rate = atoi(optarg) * 1000;
526 recording_time = atof(optarg);
532 audio_freq = atoi(optarg);
537 audio_bit_rate = atoi(optarg) * 1000;
548 audio_channels = atoi(optarg);
549 if (audio_channels != 1 &&
550 audio_channels != 2) {
551 fprintf(stderr, "Incorrect number of channels: %d\n", audio_channels);
556 /* advanced options */
561 gop_size = atoi(optarg);
567 comment_string = optarg;
574 if (optind >= argc) {
579 filename = argv[optind++];
580 video_filename = NULL;
581 audio_filename = NULL;
583 /* auto detect format */
584 if (file_format == NULL)
585 file_format = guess_format(NULL, filename, NULL);
587 if (file_format == NULL)
588 file_format = &mpeg_mux_format;
590 /* check parameters */
591 if (frame_width <= 0 || frame_height <= 0) {
592 fprintf(stderr, "Incorrect frame size\n");
595 if ((frame_width % 16) != 0 || (frame_height % 16) != 0) {
596 fprintf(stderr, "Frame size must be a multiple of 16\n");
600 if (bit_rate < 5000 || bit_rate >= 10000000) {
601 fprintf(stderr, "Invalid bit rate\n");
605 if (frame_rate < 1 || frame_rate >= 60) {
606 fprintf(stderr, "Invalid frame rate\n");
610 nb_frames = (int)(recording_time * frame_rate);
612 fprintf(stderr, "Invalid recording time\n");
616 use_video = file_format->video_codec != CODEC_ID_NONE;
617 use_audio = file_format->audio_codec != CODEC_ID_NONE;
625 if (use_video == 0 && use_audio == 0) {
626 fprintf(stderr, "No audio or video selected\n");
630 fprintf(stderr, "Recording: %s, %0.1f seconds\n",
634 /* open output media */
636 if (strstart(filename, "udp:", NULL)) {
637 output_type = OUT_UDP;
639 memset(udp_ctx, 0, sizeof(*udp_ctx));
640 if (udp_tx_open(udp_ctx, filename, 0) < 0) {
641 fprintf(stderr, "Could not open UDP socket\n");
644 } else if (!strcmp(filename, "-")) {
645 output_type = OUT_PIPE;
648 output_type = OUT_FILE;
649 outfile = fopen(filename, "w");
656 av_ctx->video_enc = NULL;
657 av_ctx->audio_enc = NULL;
659 if (output_type == OUT_UDP) {
660 init_put_byte(&av_ctx->pb, output_buffer, sizeof(output_buffer),
661 udp_ctx, udp_write_data, NULL);
663 init_put_byte(&av_ctx->pb, output_buffer, sizeof(output_buffer),
664 outfile, raw_write_data, raw_seek);
669 video_filename = argv[optind++];
671 /* init mpeg video encoding context */
672 memset(video_enc, 0, sizeof(*video_enc));
673 video_enc->bit_rate = bit_rate;
674 video_enc->rate = frame_rate;
676 video_enc->width = frame_width;
677 video_enc->height = frame_height;
679 video_enc->gop_size = gop_size;
681 video_enc->gop_size = 0;
683 av_ctx->video_enc = video_enc;
684 av_ctx->format = file_format;
689 audio_filename = argv[optind++];
691 audio_enc->bit_rate = audio_bit_rate;
692 audio_enc->rate = audio_freq;
693 audio_enc->channels = audio_channels;
694 av_ctx->audio_enc = audio_enc;
696 av_ctx->format = file_format;
697 av_ctx->is_streamed = 0;
699 av_encode(av_ctx, video_filename, audio_filename);
701 /* close output media */
703 switch(output_type) {
710 udp_tx_close(udp_ctx);
713 fprintf(stderr, "\n");