int video_combine(char* filelist[],int fileCnt) { char* outfilename=filelist[fileCnt-1]; videoFile outVideo; videoFile inVideo; open_video(filelist[1],&inVideo); avformat_alloc_output_context2(&(outVideo.pFormatCtx), NULL, NULL, outfilename); AVFormatContext* pFormatCtx=outVideo.pFormatCtx; init_output(&inVideo,outfilename,&outVideo); av_dump_format(outVideo.pFormatCtx,0,outfilename,1); if (avio_open(&(pFormatCtx->pb),outfilename, AVIO_FLAG_READ_WRITE) < 0) { printf("输出文件打开失败"); return -1; } if(avformat_write_header(pFormatCtx,NULL)<0){fprintf(stderr,"写入文件头失败\n");return -1;} int i=0; for(i=1;i<fileCnt-1;i++) { open_video(filelist[i],&inVideo); write_video2(&inVideo,&outVideo); } if(av_write_trailer(pFormatCtx)!=0){ERROR("写入文件尾失败\n");} avio_close(pFormatCtx->pb); avformat_free_context(pFormatCtx); return 0; }
Data(const std::string &filename, const std::string &fourcc, double fps, const Size &frame_size): video_st(),filename(filename),fps(fps),frame_size(frame_size){ DEBUG_LOG("fps:" << fps); if(File(filename).exists()){ throw ICLException("file already exists"); } av_register_all(); if(fourcc.length()) fmt = av_guess_format(fourcc.c_str(), filename.c_str(), 0); else fmt = av_guess_format(0, filename.c_str(), 0); if(!fmt) throw ICLException("Unkown format"); oc = avformat_alloc_context(); if (!oc) throw ICLException("Memory error"); oc->oformat = fmt; snprintf(oc->filename, sizeof(oc->filename), "%s", filename.c_str()); add_video_stream(&video_st, oc, fmt->video_codec); open_video(oc, &video_st); av_dump_format(oc, 0, filename.c_str(), 1); if (!(fmt->flags & AVFMT_NOFILE)) { if (avio_open(&oc->pb, filename.c_str(), AVIO_FLAG_WRITE) < 0) { throw ICLException("Could not open file"); } } avformat_write_header(oc, 0); }
bool video_recording_state_t::initialize(uint16 video_nr, int width, int height, int depth) { enum AVPixelFormat raw_fmt; if (depth == VIDEO_DEPTH_8BIT) raw_fmt = AV_PIX_FMT_PAL8; else if (depth == VIDEO_DEPTH_32BIT) raw_fmt = AV_PIX_FMT_ARGB; else return false; char filename[32]; snprintf(filename, sizeof filename, "rec%hu.avi", video_nr); AVOutputFormat *fmt = av_guess_format(NULL, filename, NULL); if (!fmt) return false; if (fmt->flags & AVFMT_NOFILE) return false; output_context = avformat_alloc_context(); if (!output_context) return false; output_context->oformat = fmt; snprintf(output_context->filename, sizeof(output_context->filename), "%s", filename); if (fmt->video_codec == AV_CODEC_ID_NONE) return false; if (!add_audio_stream(AV_CODEC_ID_PCM_S16LE)) return false; if (!add_video_stream(fmt->video_codec, width, height)) return false; if (!open_audio()) return false; if (!open_video()) return false; if (!(video_frame_raw = alloc_picture(video_stream, raw_fmt))) return false; if (!(video_frame = alloc_picture(video_stream, AV_PIX_FMT_YUV420P))) return false; if (!init_sws_context()) return false; if (avio_open(&output_context->pb, filename, AVIO_FLAG_WRITE) < 0) return false; avformat_write_header(output_context, NULL); return true; }
int main(int argc, char **argv) { process_args(argc, argv); open_video(); copy_frames(); return 0; }
static int tfile (GeglProperties *o) { Priv *p = (Priv*)o->user_data; p->fmt = av_guess_format (NULL, o->path, NULL); if (!p->fmt) { fprintf (stderr, "ff_save couldn't deduce outputformat from file extension: using MPEG.\n%s", ""); p->fmt = av_guess_format ("mpeg", NULL, NULL); } p->oc = avformat_alloc_context (); if (!p->oc) { fprintf (stderr, "memory error\n%s", ""); return -1; } p->oc->oformat = p->fmt; snprintf (p->oc->filename, sizeof (p->oc->filename), "%s", o->path); p->video_st = NULL; p->audio_st = NULL; if (p->fmt->video_codec != AV_CODEC_ID_NONE) { p->video_st = add_video_stream (o, p->oc, p->fmt->video_codec); } if (p->fmt->audio_codec != AV_CODEC_ID_NONE) { p->audio_st = add_audio_stream (o, p->oc, p->fmt->audio_codec); } if (p->video_st) open_video (p, p->oc, p->video_st); if (p->audio_st) open_audio (o, p->oc, p->audio_st); av_dump_format (p->oc, 0, o->path, 1); if (avio_open (&p->oc->pb, o->path, AVIO_FLAG_WRITE) < 0) { fprintf (stderr, "couldn't open '%s'\n", o->path); return -1; } avformat_write_header (p->oc, NULL); return 0; }
void CVideoLivRecord::InitRecoder(LPCSTR lpFileName,LONG lWidth,LONG lHeight,INT iKeyFrameInterval,int iOnlyVideo) { m_Width = lWidth; m_Height = lHeight; m_videoduriation = iKeyFrameInterval; m_audioduriation = iKeyFrameInterval; int ret = 0; char filename[MAX_PATH] = {0}; memcpy(filename, lpFileName, strlen(lpFileName)); // strcat(filename, "."); // strcat(filename, FILE_SUFFIX); avformat_alloc_output_context2(&m_pAVFormatContext, NULL, NULL, filename); if (!m_pAVFormatContext){ log("[CVideoLivRecord::InitRecoder] -- avformat_alloc_output_context2() error"); return ; } //video if (m_pAVFormatContext->oformat->video_codec != AV_CODEC_ID_NONE){ add_stream(&m_pVideoStream, &m_pVideoCodec, m_pAVFormatContext->oformat->video_codec); m_bHasVideo = TRUE; m_bEncodeVideo = TRUE; } //audio if (iOnlyVideo == 0 && m_pAVFormatContext->oformat->audio_codec != AV_CODEC_ID_NONE){ add_stream(&m_pAudioStream, &m_pAudioCodec, m_pAVFormatContext->oformat->audio_codec); m_bHasAudio = TRUE; m_bEncodeAudio = TRUE; } if (m_bHasVideo){ open_video(m_pVideoStream, m_pVideoCodec, m_pOpt); } if (m_bHasAudio){ open_audio(m_pAudioStream, m_pAudioCodec, m_pOpt); } if (!(m_pAVFormatContext->oformat->flags & AVFMT_NOFILE)){ ret = avio_open(&m_pAVFormatContext->pb, filename, AVIO_FLAG_WRITE); if (ret < 0){ log("[CVideoLivRecord::InitRecoder] -- avio_open() error"); return ; } } ret = avformat_write_header(m_pAVFormatContext, &m_pOpt); if (ret < 0){ log("[CVideoLivRecord::InitRecoder] -- avformat_write_header() error"); return ; } }
/*视频合成主调用*/ int video_combine2(char* filelist[],int fileCnt) { char* outfilename=filelist[fileCnt-1]; videoFile outVideo; videoFile inVideo; open_video(filelist[1],&inVideo); avformat_alloc_output_context2(&(outVideo.pFormatCtx), NULL, NULL, outfilename); AVFormatContext* pFormatCtx=outVideo.pFormatCtx; init_output(&inVideo,outfilename,&outVideo); av_dump_format(outVideo.pFormatCtx,0,outfilename,1); if (avio_open(&(pFormatCtx->pb),outfilename, AVIO_FLAG_READ_WRITE) < 0) { printf("输出文件打开失败"); return -1; } if(avformat_write_header(pFormatCtx,NULL)<0){ERROR("写入文件尾失败");} AVFrame* frameArr[10000]; int index[10]={0}; int i=0; for(i=1;i<fileCnt-1;i++) { open_video(filelist[i],&inVideo); //printf("%d\n",read_video(&inVideo,frameArr,index[i-1])); if(i==1){index[i]=read_video(&inVideo,frameArr,0)-1;} else{index[i]=read_video(&inVideo,frameArr,index[i-1]+1)+index[i-1];} } //STOP; //printf("%d %d %d %d\n",index[0],index[1],index[2],index[3]); write_video3(&outVideo,&inVideo,frameArr,index,fileCnt-2); if(av_write_trailer(pFormatCtx)!=0){ERROR("写入文件尾失败");} avio_close(pFormatCtx->pb); avformat_free_context(pFormatCtx); }
int start_up(EncoderJob &jobSpec) { jobSpec.p = new Pests(); jobSpec.oc = avformat_alloc_context(); if (!jobSpec.oc) { fprintf(stderr, "Memory error\n"); jobSpec.IsValid = false; return 3; } jobSpec.oc->oformat = jobSpec.fmt; sprintf(jobSpec.oc->filename, "%s-%05u.ts", jobSpec.BaseDirectory, jobSpec.SegmentNumber); // Set video codecs: jobSpec.fmt->video_codec = CODEC_ID_H264; // Video codec. Requires FFmpeg to be built with libx264. jobSpec.fmt->audio_codec = CODEC_ID_MP3; //CODEC_ID_AAC; // AAC is not working so well. Will use MP3 instead. jobSpec.video_st = NULL; jobSpec.audio_st = NULL; if (jobSpec.fmt->video_codec != CODEC_ID_NONE) { jobSpec.video_st = add_video_stream(jobSpec.oc, jobSpec.fmt->video_codec, jobSpec); } if (jobSpec.fmt->audio_codec != CODEC_ID_NONE) { jobSpec.audio_st = add_audio_stream(jobSpec, jobSpec.oc, jobSpec.fmt->audio_codec); } /*if (av_set_parameters(jobSpec.oc, NULL) < 0) { fprintf(stderr, "Invalid output format parameters\n"); jobSpec.IsValid = false; return 4; }*/ /* now that all the parameters are set, we can open the audio and video codecs and allocate the necessary encode buffers */ if (jobSpec.video_st) { open_video(jobSpec, jobSpec.oc, jobSpec.video_st); } if (jobSpec.audio_st) { open_audio(jobSpec.oc, jobSpec.audio_st); } #ifdef NEW_M2TS jobSpec.fmt->flags |= AVFMT_NOFILE; // we'll write our own, thanks! int track_ids[2] = {120, 121}; uint8_t track_types[2] = {Pests::TT_H264, Pests::TT_MpegAudio}; jobSpec.p->StartFile(jobSpec.oc->filename, track_ids, track_types, 2); // 120 = video, 121 = audio #else // open the output file, if needed if (!(jobSpec.fmt->flags & AVFMT_NOFILE)) { if (url_fopen(&jobSpec.oc->pb, jobSpec.oc->filename, URL_WRONLY) < 0) { fprintf(stderr, "Could not open '%s'\n", jobSpec.oc->filename); jobSpec.IsValid = false; return 5; } av_write_header(jobSpec.oc); } #endif // All done OK, validate and return. // From this point on, the developer MUST call CloseEncoderJob() before exiting. jobSpec.IsValid = true; return 0; }
/* media file output */ int main(int argc, char **argv) { const char *filename; AVOutputFormat *fmt; AVFormatContext *oc; AVStream *video_st = NULL; AVCodec *video_codec = NULL; double video_time; int flush, ret; /* Initialize libavcodec, and register all codecs and formats. */ av_register_all(); filename = "E:\\muxing.mp4"; /* allocate the output media context */ avformat_alloc_output_context2(&oc, NULL, NULL, filename); if (!oc) { printf("Could not deduce output format from file extension: using MPEG.\n"); avformat_alloc_output_context2(&oc, NULL, "mpeg", filename); } if (!oc) return 1; fmt = oc->oformat; //muxing 할 때 outputformat 설정 /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ //fmt->video_codec = AV_CODEC_ID_H264; fmt->video_codec = AV_CODEC_ID_MPEG4; if (fmt->video_codec != AV_CODEC_ID_NONE) video_st = add_stream(oc, &video_codec, fmt->video_codec); // add_stream(AVFormatContext *oc, AVCodec **codec,enum AVCodecID codec_id) // codec parameters set 함수 /* Now that all the parameters are set, we can open the audio and * video codecs and allocate the necessary encode buffers. */ if (video_st) open_video(oc, video_codec, video_st); // (AVFormatContext *oc, AVCodec *codec, AVStream *st) // 코댁 열기, 프레임 설정 av_dump_format(oc, 0, filename, 1); // 정보 출력 디버깅 함수 /* open the output file, if needed */ if (!(fmt->flags & AVFMT_NOFILE)) { ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE); if (ret < 0) { char buf[256]; av_strerror(ret, buf, sizeof(buf)); fprintf(stderr, "Could not open '%s': %s\n", filename, buf); return 1; } } /* Write the stream header, if any. */ ret = avformat_write_header(oc, NULL); // Allocate the stream private data and write the stream header to an output media file. // 헤더파일 생성 -> 본체 생성 -> 마무리 작업 if (ret < 0) { char buf[256]; av_strerror(ret, buf, sizeof(buf)); fprintf(stderr, "Error occurred when opening output file: %s\n", buf); return 1; } flush = 0; while ((video_st && !video_is_eof)) { if (!flush && (!video_st)) { flush = 1; } if (video_st && !video_is_eof) { write_video_frame(oc, video_st, flush); // 본체 생성 } if (frame_count == 10000) break; } /* Write the trailer, if any. The trailer must be written before you * close the CodecContexts open when you wrote the header; otherwise * av_write_trailer() may try to use memory that was freed on * av_codec_close(). */ av_write_trailer(oc); /* Close each codec. */ if (video_st) close_video(oc, video_st); if (!(fmt->flags & AVFMT_NOFILE)) /* Close the output file. */ avio_close(oc->pb); /* free the stream */ avformat_free_context(oc); return 0; }
int ff_example(const char *filename, const char *format) { AVOutputFormat *fmt; AVFormatContext *oc; AVStream *video_st; double video_pts; int i; fmt = av_guess_format(format, NULL, NULL); if (!fmt) { fprintf(stderr, "Could not find suitable output format\n"); exit(1); } fmt->video_codec = CODEC_ID_MJPEG; /* allocate the output media context */ oc = avformat_alloc_context(); if (!oc) { fprintf(stderr, "Memory error\n"); exit(1); } oc->oformat = fmt; snprintf(oc->filename, sizeof(oc->filename), "%s", filename); video_st = NULL; if (fmt->video_codec != CODEC_ID_NONE) video_st = add_video_stream(oc, fmt->video_codec); av_dump_format(oc, 0, filename, 1); /* now that all the parameters are set, we can open the audio and video codecs and allocate the necessary encode buffers */ if (video_st) open_video(oc, video_st); if (avio_open(&oc->pb, filename, URL_WRONLY) < 0) { fprintf(stderr, "Could not open '%s'\n", filename); exit(1); } /* write the stream header, if any */ avformat_write_header(oc, NULL); for(;;) { video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den; printf("pts: %f\n", video_pts); if (frame_count > STREAM_NB_FRAMES) break; /* write interleaved audio and video frames */ if (write_video_frame(oc, video_st) < 0) break; } printf("%d frames written\n", frame_count); av_write_trailer(oc); /* close each codec */ if (video_st) close_video(oc, video_st); /* free the streams */ for(i = 0; i < oc->nb_streams; i++) { av_freep(&oc->streams[i]->codec); av_freep(&oc->streams[i]); } avio_close(oc->pb); /* free the stream */ av_free(oc); return 0; }
int main(int argc,char *argv[]){ int i,j; ogg_packet op; FILE *infile = stdin; #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */ /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode( _fileno( stdin ), _O_BINARY ); #endif /* open the input file if any */ if(argc==2){ infile=fopen(argv[1],"rb"); if(infile==NULL){ fprintf(stderr,"Unable to open '%s' for playback.\n", argv[1]); exit(1); } } if(argc>2){ usage(); exit(1); } /* start up Ogg stream synchronization layer */ ogg_sync_init(&oy); /* init supporting Vorbis structures needed in header parsing */ vorbis_info_init(&vi); vorbis_comment_init(&vc); /* init supporting Theora structures needed in header parsing */ theora_comment_init(&tc); theora_info_init(&ti); /* Ogg file open; parse the headers */ /* Only interested in Vorbis/Theora streams */ while(!stateflag){ int ret=buffer_data(infile,&oy); if(ret==0)break; while(ogg_sync_pageout(&oy,&og)>0){ ogg_stream_state test; /* is this a mandated initial header? If not, stop parsing */ if(!ogg_page_bos(&og)){ /* don't leak the page; get it into the appropriate stream */ queue_page(&og); stateflag=1; break; } ogg_stream_init(&test,ogg_page_serialno(&og)); ogg_stream_pagein(&test,&og); ogg_stream_packetout(&test,&op); /* identify the codec: try theora */ if(!theora_p && theora_decode_header(&ti,&tc,&op)>=0){ /* it is theora */ memcpy(&to,&test,sizeof(test)); theora_p=1; }else if(!vorbis_p && vorbis_synthesis_headerin(&vi,&vc,&op)>=0){ /* it is vorbis */ memcpy(&vo,&test,sizeof(test)); vorbis_p=1; }else{ /* whatever it is, we don't care about it */ ogg_stream_clear(&test); } } /* fall through to non-bos page parsing */ } /* we're expecting more header packets. */ while((theora_p && theora_p<3) || (vorbis_p && vorbis_p<3)){ int ret; /* look for further theora headers */ while(theora_p && (theora_p<3) && (ret=ogg_stream_packetout(&to,&op))){ if(ret<0){ fprintf(stderr,"Error parsing Theora stream headers; corrupt stream?\n"); exit(1); } if(theora_decode_header(&ti,&tc,&op)){ printf("Error parsing Theora stream headers; corrupt stream?\n"); exit(1); } theora_p++; if(theora_p==3)break; } /* look for more vorbis header packets */ while(vorbis_p && (vorbis_p<3) && (ret=ogg_stream_packetout(&vo,&op))){ if(ret<0){ fprintf(stderr,"Error parsing Vorbis stream headers; corrupt stream?\n"); exit(1); } if(vorbis_synthesis_headerin(&vi,&vc,&op)){ fprintf(stderr,"Error parsing Vorbis stream headers; corrupt stream?\n"); exit(1); } vorbis_p++; if(vorbis_p==3)break; } /* The header pages/packets will arrive before anything else we care about, or the stream is not obeying spec */ if(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); /* demux into the appropriate stream */ }else{ int ret=buffer_data(infile,&oy); /* someone needs more data */ if(ret==0){ fprintf(stderr,"End of file while searching for codec headers.\n"); exit(1); } } } /* and now we have it all. initialize decoders */ if(theora_p){ theora_decode_init(&td,&ti); printf("Ogg logical stream %x is Theora %dx%d %.02f fps video\n", (unsigned int)to.serialno,ti.width,ti.height, (double)ti.fps_numerator/ti.fps_denominator); if(ti.width!=ti.frame_width || ti.height!=ti.frame_height) printf(" Frame content is %dx%d with offset (%d,%d).\n", ti.frame_width, ti.frame_height, ti.offset_x, ti.offset_y); report_colorspace(&ti); dump_comments(&tc); }else{ /* tear down the partial theora setup */ theora_info_clear(&ti); theora_comment_clear(&tc); } if(vorbis_p){ vorbis_synthesis_init(&vd,&vi); vorbis_block_init(&vd,&vb); fprintf(stderr,"Ogg logical stream %x is Vorbis %d channel %d Hz audio.\n", (unsigned int)vo.serialno,vi.channels,(int)vi.rate); }else{ /* tear down the partial vorbis setup */ vorbis_info_clear(&vi); vorbis_comment_clear(&vc); } /* open audio */ if(vorbis_p)open_audio(); /* open video */ if(theora_p)open_video(); /* install signal handler as SDL clobbered the default */ signal (SIGINT, sigint_handler); /* on to the main decode loop. We assume in this example that audio and video start roughly together, and don't begin playback until we have a start frame for both. This is not necessarily a valid assumption in Ogg A/V streams! It will always be true of the example_encoder (and most streams) though. */ stateflag=0; /* playback has not begun */ while(!got_sigint){ /* we want a video and audio frame ready to go at all times. If we have to buffer incoming, buffer the compressed data (ie, let ogg do the buffering) */ while(vorbis_p && !audiobuf_ready){ int ret; float **pcm; /* if there's pending, decoded audio, grab it */ if((ret=vorbis_synthesis_pcmout(&vd,&pcm))>0){ int count=audiobuf_fill/2; int maxsamples=(audiofd_fragsize-audiobuf_fill)/2/vi.channels; for(i=0;i<ret && i<maxsamples;i++) for(j=0;j<vi.channels;j++){ int val=rint(pcm[j][i]*32767.f); if(val>32767)val=32767; if(val<-32768)val=-32768; audiobuf[count++]=val; } vorbis_synthesis_read(&vd,i); audiobuf_fill+=i*vi.channels*2; if(audiobuf_fill==audiofd_fragsize)audiobuf_ready=1; if(vd.granulepos>=0) audiobuf_granulepos=vd.granulepos-ret+i; else audiobuf_granulepos+=i; }else{ /* no pending audio; is there a pending packet to decode? */ if(ogg_stream_packetout(&vo,&op)>0){ if(vorbis_synthesis(&vb,&op)==0) /* test for success! */ vorbis_synthesis_blockin(&vd,&vb); }else /* we need more data; break out to suck in another page */ break; } } while(theora_p && !videobuf_ready){ /* theora is one in, one out... */ if(ogg_stream_packetout(&to,&op)>0){ theora_decode_packetin(&td,&op); videobuf_granulepos=td.granulepos; videobuf_time=theora_granule_time(&td,videobuf_granulepos); /* is it already too old to be useful? This is only actually useful cosmetically after a SIGSTOP. Note that we have to decode the frame even if we don't show it (for now) due to keyframing. Soon enough libtheora will be able to deal with non-keyframe seeks. */ if(videobuf_time>=get_time()) videobuf_ready=1; }else break; } if(!videobuf_ready && !audiobuf_ready && feof(infile))break; if(!videobuf_ready || !audiobuf_ready){ /* no data yet for somebody. Grab another page */ int bytes=buffer_data(infile,&oy); while(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } } /* If playback has begun, top audio buffer off immediately. */ if(stateflag) audio_write_nonblocking(); /* are we at or past time for this video frame? */ if(stateflag && videobuf_ready && videobuf_time<=get_time()){ video_write(); videobuf_ready=0; } if(stateflag && (audiobuf_ready || !vorbis_p) && (videobuf_ready || !theora_p) && !got_sigint){ /* we have an audio frame ready (which means the audio buffer is full), it's not time to play video, so wait until one of the audio buffer is ready or it's near time to play video */ /* set up select wait on the audiobuffer and a timeout for video */ struct timeval timeout; fd_set writefs; fd_set empty; int n=0; FD_ZERO(&writefs); FD_ZERO(&empty); if(audiofd>=0){ FD_SET(audiofd,&writefs); n=audiofd+1; } if(theora_p){ long milliseconds=(videobuf_time-get_time())*1000-5; if(milliseconds>500)milliseconds=500; if(milliseconds>0){ timeout.tv_sec=milliseconds/1000; timeout.tv_usec=(milliseconds%1000)*1000; n=select(n,&empty,&writefs,&empty,&timeout); if(n)audio_calibrate_timer(0); } }else{ select(n,&empty,&writefs,&empty,NULL); } } /* if our buffers either don't exist or are ready to go, we can begin playback */ if((!theora_p || videobuf_ready) && (!vorbis_p || audiobuf_ready))stateflag=1; /* same if we've run out of input */ if(feof(infile))stateflag=1; } /* tear it all down */ audio_close(); SDL_Quit(); if(vorbis_p){ ogg_stream_clear(&vo); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_comment_clear(&vc); vorbis_info_clear(&vi); } if(theora_p){ ogg_stream_clear(&to); theora_clear(&td); theora_comment_clear(&tc); theora_info_clear(&ti); } ogg_sync_clear(&oy); if(infile && infile!=stdin)fclose(infile); fprintf(stderr, "\r " "\nDone.\n"); return(0); }
int main(int argc, char **argv) { OutputStream video_st = { 0 }, audio_st = { 0 }; const char *filename; AVOutputFormat *fmt; AVFormatContext *oc; int have_video = 0, have_audio = 0; int encode_video = 0, encode_audio = 0; /* Initialize libavcodec, and register all codecs and formats. */ av_register_all(); if (argc != 2) { printf("usage: %s output_file\n" "API example program to output a media file with libavformat.\n" "The output format is automatically guessed according to the file extension.\n" "Raw images can also be output by using '%%d' in the filename\n" "\n", argv[0]); return 1; } filename = argv[1]; /* Autodetect the output format from the name. default is MPEG. */ fmt = av_guess_format(NULL, filename, NULL); if (!fmt) { printf("Could not deduce output format from file extension: using MPEG.\n"); fmt = av_guess_format("mpeg", NULL, NULL); } if (!fmt) { fprintf(stderr, "Could not find suitable output format\n"); return 1; } /* Allocate the output media context. */ oc = avformat_alloc_context(); if (!oc) { fprintf(stderr, "Memory error\n"); return 1; } oc->oformat = fmt; snprintf(oc->filename, sizeof(oc->filename), "%s", filename); /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ if (fmt->video_codec != AV_CODEC_ID_NONE) { add_video_stream(&video_st, oc, fmt->video_codec); have_video = 1; encode_video = 1; } if (fmt->audio_codec != AV_CODEC_ID_NONE) { add_audio_stream(&audio_st, oc, fmt->audio_codec); have_audio = 1; encode_audio = 1; } /* Now that all the parameters are set, we can open the audio and * video codecs and allocate the necessary encode buffers. */ if (have_video) open_video(oc, &video_st); if (have_audio) open_audio(oc, &audio_st); av_dump_format(oc, 0, filename, 1); /* open the output file, if needed */ if (!(fmt->flags & AVFMT_NOFILE)) { if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) { fprintf(stderr, "Could not open '%s'\n", filename); return 1; } } /* Write the stream header, if any. */ avformat_write_header(oc, NULL); while (encode_video || encode_audio) { /* select the stream to encode */ if (encode_video && (!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base, audio_st.next_pts, audio_st.enc->time_base) <= 0)) { encode_video = !write_video_frame(oc, &video_st); } else { encode_audio = !process_audio_stream(oc, &audio_st); } } /* Write the trailer, if any. The trailer must be written before you * close the CodecContexts open when you wrote the header; otherwise * av_write_trailer() may try to use memory that was freed on * av_codec_close(). */ av_write_trailer(oc); /* Close each codec. */ if (have_video) close_stream(oc, &video_st); if (have_audio) close_stream(oc, &audio_st); if (!(fmt->flags & AVFMT_NOFILE)) /* Close the output file. */ avio_close(oc->pb); /* free the stream */ avformat_free_context(oc); return 0; }
/* * Initialize the AVFormatContext * Called on encoder initialize and when beginning * each new video chunk */ int initializeAVFormatContext(AVFormatContext **out_oc, jbyte *output_filename, AVStream **out_video_st, AVFrame **out_picture, int video_width, int video_height, float video_crf, int *out_last_pts, int *out_video_frame_count, AVStream **out_audio_st, int16_t **out_samples, int audio_bitrate){ AVFormatContext *oc; AVStream *video_st; AVStream *audio_st; AVFrame *picture; int16_t *samples; // TODO: Can we do this only once? /* Initialize libavcodec, and register all codecs and formats. */ av_register_all(); //LOGI("initializeAVFC with filename: %s", output_filename); if(!oc) LOGI("initializeAVFC, oc is properly null"); /* allocate the output media context */ avformat_alloc_output_context2(&oc, NULL, NULL, ((const char*) output_filename)); if (!oc) { LOGI("Could not deduce output format, using mpeg"); //printf("Could not deduce output format from file extension: using MPEG.\n"); avformat_alloc_output_context2(&oc, NULL, "mpeg", ((const char*) output_filename)); } if (!oc) { LOGE("Could not allocate output context"); exit(1); } //else //LOGI("initializeAVFC, oc appears properly allocated"); //LOGI("avformat_alloc_output_context2"); fmt = oc->oformat; // Set AVOutputFormat video/audio codec fmt->video_codec = VIDEO_CODEC_ID; fmt->audio_codec = AUDIO_CODEC_ID; /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ video_st = NULL; audio_st = NULL; if (fmt->video_codec != CODEC_ID_NONE) { video_st = add_video_stream(oc, fmt->video_codec, video_width, video_height, video_crf); //(AVFormatContext *oc, enum CodecID codec_id, int width, int height, float crf) } if (fmt->audio_codec != CODEC_ID_NONE) { audio_st = add_audio_stream(oc, fmt->audio_codec, audio_bitrate); //static AVStream *add_audio_stream(AVFormatContext *oc, enum CodecID codec_id, int bit_rate) } //LOGI("add_audio_stream / add_video_stream"); /* Now that all the parameters are set, we can open the audio and * video codecs and allocate the necessary encode buffers. */ if (video_st){ open_video(oc, video_st, &picture); //open_video(AVFormatContext *oc, AVStream *st, AVFrame *picture } if (audio_st){ open_audio(oc, audio_st ,&samples); //open_audio(AVFormatContext *oc, AVStream *st, int16_t *samples) } av_dump_format(oc, 0, output_filename, 1); //LOGI("open audio / video"); /* open the output file, if needed */ if (!(fmt->flags & AVFMT_NOFILE)) { char *error_buffer_ptr; char error_buffer[90]; error_buffer_ptr = error_buffer; //LOGI("pre avio_open2"); int error = avio_open2(&oc->pb, output_filename, AVIO_FLAG_WRITE, NULL, NULL); //LOGI("post avio_open2"); if ( error < 0) { av_strerror (error, error_buffer_ptr, 90); LOGE("Could not open %s. Error: %s", output_filename, error_buffer_ptr); //fprintf(stderr, "Could not open '%s'\n", native_output_file_lq1); exit(-420); } } /* Write the stream header, if any. */ //LOGI("pre avformat_write_header"); avformat_write_header(oc, NULL); //LOGI("avformat_write_header"); //LOGI("end initializeAVFC: audio_input_frame_size: %d fps: %d", audio_input_frame_size, video_st->codec->time_base.den); // Set results to output arguments *out_oc = oc; *out_video_st = video_st; *out_audio_st = audio_st; *out_picture = picture; *out_samples = samples; *out_last_pts = -1; *out_video_frame_count = 0; return audio_input_frame_size; }
int main(int argc, char **argv) { const char *filename; AVOutputFormat *fmt; AVFormatContext *oc; AVStream *audio_st, *video_st; double audio_pts, video_pts; int i; /* Initialize libavcodec, and register all codecs and formats. */ av_register_all(); if (argc != 2) { printf("usage: %s output_file\n" "API example program to output a media file with libavformat.\n" "The output format is automatically guessed according to the file extension.\n" "Raw images can also be output by using '%%d' in the filename\n" "\n", argv[0]); return 1; } filename = argv[1]; /* Autodetect the output format from the name. default is MPEG. */ fmt = av_guess_format(NULL, filename, NULL); if (!fmt) { printf("Could not deduce output format from file extension: using MPEG.\n"); fmt = av_guess_format("mpeg", NULL, NULL); } if (!fmt) { fprintf(stderr, "Could not find suitable output format\n"); return 1; } /* Allocate the output media context. */ oc = avformat_alloc_context(); if (!oc) { fprintf(stderr, "Memory error\n"); return 1; } oc->oformat = fmt; snprintf(oc->filename, sizeof(oc->filename), "%s", filename); /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ video_st = NULL; audio_st = NULL; if (fmt->video_codec != AV_CODEC_ID_NONE) { video_st = add_video_stream(oc, fmt->video_codec); } if (fmt->audio_codec != AV_CODEC_ID_NONE) { audio_st = add_audio_stream(oc, fmt->audio_codec); } /* Now that all the parameters are set, we can open the audio and * video codecs and allocate the necessary encode buffers. */ if (video_st) open_video(oc, video_st); if (audio_st) open_audio(oc, audio_st); av_dump_format(oc, 0, filename, 1); /* open the output file, if needed */ if (!(fmt->flags & AVFMT_NOFILE)) { if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) { fprintf(stderr, "Could not open '%s'\n", filename); return 1; } } /* Write the stream header, if any. */ avformat_write_header(oc, NULL); for (;;) { /* Compute current audio and video time. */ if (audio_st) audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den; else audio_pts = 0.0; if (video_st) video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den; else video_pts = 0.0; if ((!audio_st || audio_pts >= STREAM_DURATION) && (!video_st || video_pts >= STREAM_DURATION)) break; /* write interleaved audio and video frames */ if (!video_st || (video_st && audio_st && audio_pts < video_pts)) { write_audio_frame(oc, audio_st); } else { write_video_frame(oc, video_st); } } /* Write the trailer, if any. The trailer must be written before you * close the CodecContexts open when you wrote the header; otherwise * av_write_trailer() may try to use memory that was freed on * av_codec_close(). */ av_write_trailer(oc); /* Close each codec. */ if (video_st) close_video(oc, video_st); if (audio_st) close_audio(oc, audio_st); /* Free the streams. */ for (i = 0; i < oc->nb_streams; i++) { av_freep(&oc->streams[i]->codec); av_freep(&oc->streams[i]); } if (!(fmt->flags & AVFMT_NOFILE)) /* Close the output file. */ avio_close(oc->pb); /* free the stream */ av_free(oc); return 0; }
int prepare(VideoOut_sV *video, const char *filename, const char *vcodec, const int width, const int height, const int bitrate, const unsigned int numerator, const unsigned int denominator) { /* must be called before using avcodec lib */ avcodec_init(); video->frameNr = 0; video->errorMessage = NULL; video->filename = malloc(strlen(filename)+1); strcpy(video->filename, filename); /* initialize libavcodec, and register all codecs and formats */ av_register_all(); /* allocate the output media context */ avformat_alloc_output_context2(&video->fc, NULL, NULL, filename); if (!video->fc) { printf("Could not deduce output format from file extension: using MPEG.\n"); avformat_alloc_output_context2(&video->fc, NULL, "mpeg", filename); } if (!video->fc) { const char *s = "Could allocate the output context, even MPEG is not available.\n"; fprintf(stderr, s); setErrorMessage(video, s); return 2; } video->format = video->fc->oformat; printf("Using format %s.\n", video->format->name); /* Use the given vcodec if it is not NULL */ if (vcodec != NULL) { AVCodec *codec = avcodec_find_encoder_by_name(vcodec); if (codec == NULL) { char s[strlen(vcodec)+50]; sprintf(s, "No codec available for %s.\n", vcodec); fprintf(stderr, s); setErrorMessage(video, s); return 2; } printf("Found codec: %s\n", codec->long_name); video->format->video_codec = codec->id; } /* add the audio and video streams using the default format codecs and initialize the codecs */ video->streamV = NULL; if (video->format->video_codec != CODEC_ID_NONE) { video->streamV = av_new_stream(video->fc, 0); if (!video->streamV) { const char *s = "Could not allocate the video stream.\n"; fprintf(stderr, s); setErrorMessage(video, s); return 2; } AVCodecContext *cc = video->streamV->codec; cc->codec_id = video->format->video_codec; cc->codec_type = AVMEDIA_TYPE_VIDEO; cc->bit_rate = bitrate; /* resolution must be a multiple of two */ cc->width = width; cc->height = height; /* time base: this is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented. for fixed-fps content, timebase should be 1/framerate and timestamp increments should be identically 1. */ cc->time_base= (AVRational){numerator, denominator}; cc->gop_size = 12; /* emit one intra frame every ten frames */ cc->pix_fmt = PIX_FMT_YUV420P; if (cc->codec_id == CODEC_ID_MPEG2VIDEO || cc->codec_id == CODEC_ID_MPEG4) { /* just for testing, we also add B frames */ cc->max_b_frames = 2; } if (cc->codec_id == CODEC_ID_MPEG1VIDEO){ /* Needed to avoid using macroblocks in which some coeffs overflow. This does not happen with normal video, it just happens here as the motion of the chroma plane does not match the luma plane. */ cc->mb_decision=2; } // some formats want stream headers to be separate if(video->fc->oformat->flags & AVFMT_GLOBALHEADER) { cc->flags |= CODEC_FLAG_GLOBAL_HEADER; } video->rgbConversionContext = sws_getContext( cc->width, cc->height, PIX_FMT_BGRA, cc->width, cc->height, cc->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL); // One line size for each plane. RGB consists of one plane only. // (YUV420p consists of 3, Y, Cb, and Cr video->rgbLinesize[0] = cc->width*4; video->rgbLinesize[1] = 0; video->rgbLinesize[2] = 0; video->rgbLinesize[3] = 0; if (video->rgbConversionContext == NULL) { char s[200]; sprintf(s, "Cannot initialize the RGB conversion context. Incorrect size (%dx%d)?\n", cc->width, cc->height); fprintf(stderr, s); setErrorMessage(video, s); return 2; } printf("Settings: %dx%d, %d bits/s (tolerance: %d), %d fps\n", cc->width, cc->height, cc->bit_rate, cc->bit_rate_tolerance, ((float)cc->time_base.den)/cc->time_base.num); fflush(stdout); } else { const char *s = "No codec ID given.\n"; fprintf(stderr, s); setErrorMessage(video, s); return 2; } av_dump_format(video->fc, 0, filename, 1); /* now that all the parameters are set, we can open the audio and video codecs and allocate the necessary encode buffers */ if (video->streamV) { int ret = open_video(video); if (ret != 0) { return ret; } } else { const char *s = "Could not open video stream.\n"; fprintf(stderr, s); setErrorMessage(video, s); return 2; } /* open the output file, if needed */ if (!(video->format->flags & AVFMT_NOFILE)) { if (avio_open(&video->fc->pb, filename, AVIO_FLAG_WRITE) < 0) { fprintf(stderr, "could not open %s\n", video->filename); char *msg = "Could not open file: "; char *msgAll = malloc(sizeof(char) * (strlen(filename) + strlen(msg))); strcpy(msgAll, msg); strcat(msgAll, filename); fprintf(stderr, msgAll); setErrorMessage(video, msgAll); free(msgAll); return 5; } } /* write the stream header, if any */ avformat_write_header(video->fc, NULL); /* alloc image and output buffer */ video->outbufSizeV = avpicture_get_size(video->streamV->codec->pix_fmt, width, height); video->outbufV = av_malloc(video->outbufSizeV); video->picture = avcodec_alloc_frame(); avpicture_alloc((AVPicture*)video->picture, video->streamV->codec->pix_fmt, video->streamV->codec->width, video->streamV->codec->height); if (!video->picture) { const char *s = "Could not allocate AVPicture.\n"; fprintf(stderr, s); setErrorMessage(video, s); return 2; } return 0; }
int main(int argc, char **argv) { OutputStream video_st = { 0 }, audio_st = { 0 }; const char *filename; AVOutputFormat *fmt; AVFormatContext *oc; AVCodec *audio_codec, *video_codec; int ret; int have_video = 0, have_audio = 0; int encode_video = 0, encode_audio = 0; AVDictionary *opt = NULL; /* Initialize libavcodec, and register all codecs and formats. */ av_register_all(); if (argc < 2) { printf("usage: %s output_file\n" "API example program to output a media file with libavformat.\n" "This program generates a synthetic audio and video stream, encodes and\n" "muxes them into a file named output_file.\n" "The output format is automatically guessed according to the file extension.\n" "Raw images can also be output by using '%%d' in the filename.\n" "\n", argv[0]); return 1; } filename = argv[1]; if (argc > 3 && !strcmp(argv[2], "-flags")) { av_dict_set(&opt, argv[2]+1, argv[3], 0); } /* allocate the output media context */ avformat_alloc_output_context2(&oc, NULL, NULL, filename); if (!oc) { printf("Could not deduce output format from file extension: using MPEG.\n"); avformat_alloc_output_context2(&oc, NULL, "mpeg", filename); } if (!oc) return 1; fmt = oc->oformat; /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ if (fmt->video_codec != AV_CODEC_ID_NONE) { add_stream(&video_st, oc, &video_codec, fmt->video_codec); have_video = 1; encode_video = 1; } if (fmt->audio_codec != AV_CODEC_ID_NONE) { add_stream(&audio_st, oc, &audio_codec, fmt->audio_codec); have_audio = 1; encode_audio = 1; } /* Now that all the parameters are set, we can open the audio and * video codecs and allocate the necessary encode buffers. */ if (have_video) open_video(oc, video_codec, &video_st, opt); if (have_audio) open_audio(oc, audio_codec, &audio_st, opt); av_dump_format(oc, 0, filename, 1); /* open the output file, if needed */ if (!(fmt->flags & AVFMT_NOFILE)) { ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE); if (ret < 0) { fprintf(stderr, "Could not open '%s': %s\n", filename, av_err2str(ret)); return 1; } } /* Write the stream header, if any. */ ret = avformat_write_header(oc, &opt); if (ret < 0) { fprintf(stderr, "Error occurred when opening output file: %s\n", av_err2str(ret)); return 1; } while (encode_video || encode_audio) { /* select the stream to encode */ if (encode_video && (!encode_audio || av_compare_ts(video_st.next_pts, video_st.st->codec->time_base, audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) { encode_video = !write_video_frame(oc, &video_st); } else { encode_audio = !write_audio_frame(oc, &audio_st); } } /* Write the trailer, if any. The trailer must be written before you * close the CodecContexts open when you wrote the header; otherwise * av_write_trailer() may try to use memory that was freed on * av_codec_close(). */ av_write_trailer(oc); /* Close each codec. */ if (have_video) close_stream(oc, &video_st); if (have_audio) close_stream(oc, &audio_st); if (!(fmt->flags & AVFMT_NOFILE)) /* Close the output file. */ avio_close(oc->pb); /* free the stream */ avformat_free_context(oc); return 0; }
int main(int argc, char **argv) { if (argc < 2) { printf("usage: %s output_file\n" "API example program to output a media file with libavformat.\n" "This program generates a synthetic video stream, encodes and\n" "muxes them into a file named output_file.\n" "The output format is automatically guessed according to the file extension.\n" "Raw images can also be output by using '%%d' in the filename.\n" "\n", argv[0]); return 1; } /* Initialize libavcodec, and register all codecs and formats. */ av_register_all(); std::string filename = argv[1]; /* allocate the output media context */ AVFormatContext *oc = NULL; avformat_alloc_output_context2(&oc, NULL, "mp4", NULL); if (!oc) { printf("fail to generate mp4 format context"); return 1; } /* Add the video streams using the default format codecs and initialize the codecs. */ OutputStream video_st = {0}; AVCodec *video_codec = NULL; if (oc->oformat->video_codec != AV_CODEC_ID_NONE) { add_stream(&video_st, oc, &video_codec, oc->oformat->video_codec); } /* Now that all the parameters are set, we can open the * video codecs and allocate the necessary encode buffers. */ AVDictionary *opt = NULL; if (video_codec != NULL) open_video(oc, video_codec, &video_st, opt); av_dump_format(oc, 0, filename.c_str(), 1); /* open the output file, if needed */ int ret = 0; if (!(oc->oformat->flags & AVFMT_NOFILE)) { if ((ret = avio_open(&oc->pb, filename.c_str(), AVIO_FLAG_WRITE)) < 0) { fprintf(stderr, "Could not open '%s': %s\n", filename.c_str(), av_err2str(ret)); return 1; } } /* Write the stream header, if any. */ AVDictionary *movflags = NULL; av_dict_set(&movflags, "movflags", "empty_moov+default_base_moof+frag_keyframe", 0); if ((ret = avformat_write_header(oc, &movflags)) < 0) { fprintf(stderr, "Error occurred when opening output file: %s\n", av_err2str(ret)); return 1; } av_dict_free(&movflags); // Generate raw video frame, encode them and mux into container bool encode_video = true; while (encode_video) { encode_video = write_video_frame(oc, &video_st); } /* Write the trailer, if any. The trailer must be written before you * close the CodecContexts open when you wrote the header; otherwise * av_write_trailer() may try to use memory that was freed on * av_codec_close(). */ av_write_trailer(oc); /* Close each codec. */ if (video_codec != NULL) close_stream(oc, &video_st); if (!(oc->oformat->flags & AVFMT_NOFILE)) /* Close the output file. */ avio_closep(&oc->pb); /* free the stream */ avformat_free_context(oc); return 0; }
int main(int argc,char *argv[]){ ogg_packet op; int long_option_index; int c; struct timeb start; struct timeb after; struct timeb last; int fps_only=0; int frames = 0; FILE *infile = stdin; outfile = stdout; #ifdef _WIN32 /* We need to set stdin/stdout to binary mode on windows. */ /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); #endif /* Process option arguments. */ while((c=getopt_long(argc,argv,optstring,options,&long_option_index))!=EOF){ switch(c){ case 'o': if(strcmp(optarg,"-")!=0){ outfile=fopen(optarg,"wb"); if(outfile==NULL){ fprintf(stderr,"Unable to open output file '%s'\n", optarg); exit(1); } }else{ outfile=stdout; } break; case 'c': crop=1; break; case 'r': raw=1; break; case 'f': fps_only = 1; outfile = NULL; break; default: usage(); } } if(optind<argc){ infile=fopen(argv[optind],"rb"); if(infile==NULL){ fprintf(stderr,"Unable to open '%s' for extraction.\n", argv[optind]); exit(1); } if(++optind<argc){ usage(); exit(1); } } /*Ok, Ogg parsing. The idea here is we have a bitstream that is made up of Ogg pages. The libogg sync layer will find them for us. There may be pages from several logical streams interleaved; we find the first theora stream and ignore any others. Then we pass the pages for our stream to the libogg stream layer which assembles our original set of packets out of them. It's the packets that libtheora actually knows how to handle.*/ /* start up Ogg stream synchronization layer */ ogg_sync_init(&oy); /* init supporting Theora structures needed in header parsing */ th_comment_init(&tc); th_info_init(&ti); /*Ogg file open; parse the headers. Theora (like Vorbis) depends on some initial header packets for decoder setup and initialization. We retrieve these first before entering the main decode loop.*/ /* Only interested in Theora streams */ while(!stateflag){ int ret=buffer_data(infile,&oy); if(ret==0)break; while(ogg_sync_pageout(&oy,&og)>0){ int got_packet; ogg_stream_state test; /* is this a mandated initial header? If not, stop parsing */ if(!ogg_page_bos(&og)){ /* don't leak the page; get it into the appropriate stream */ queue_page(&og); stateflag=1; break; } ogg_stream_init(&test,ogg_page_serialno(&og)); ogg_stream_pagein(&test,&og); got_packet = ogg_stream_packetpeek(&test,&op); /* identify the codec: try theora */ if((got_packet==1) && !theora_p && (theora_processing_headers= th_decode_headerin(&ti,&tc,&ts,&op))>=0){ /* it is theora -- save this stream state */ memcpy(&to,&test,sizeof(test)); theora_p=1; /*Advance past the successfully processed header.*/ if(theora_processing_headers)ogg_stream_packetout(&to,NULL); }else{ /* whatever it is, we don't care about it */ ogg_stream_clear(&test); } } /* fall through to non-bos page parsing */ } /* we're expecting more header packets. */ while(theora_p && theora_processing_headers){ int ret; /* look for further theora headers */ while(theora_processing_headers&&(ret=ogg_stream_packetpeek(&to,&op))){ if(ret<0)continue; theora_processing_headers=th_decode_headerin(&ti,&tc,&ts,&op); if(theora_processing_headers<0){ fprintf(stderr,"Error parsing Theora stream headers; " "corrupt stream?\n"); exit(1); } else if(theora_processing_headers>0){ /*Advance past the successfully processed header.*/ ogg_stream_packetout(&to,NULL); } theora_p++; } /*Stop now so we don't fail if there aren't enough pages in a short stream.*/ if(!(theora_p && theora_processing_headers))break; /* The header pages/packets will arrive before anything else we care about, or the stream is not obeying spec */ if(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); /* demux into the appropriate stream */ }else{ int ret=buffer_data(infile,&oy); /* someone needs more data */ if(ret==0){ fprintf(stderr,"End of file while searching for codec headers.\n"); exit(1); } } } /* and now we have it all. initialize decoders */ if(theora_p){ dump_comments(&tc); td=th_decode_alloc(&ti,ts); fprintf(stderr,"Ogg logical stream %lx is Theora %dx%d %.02f fps video\n" "Encoded frame content is %dx%d with %dx%d offset\n", to.serialno,ti.frame_width,ti.frame_height, (double)ti.fps_numerator/ti.fps_denominator, ti.pic_width,ti.pic_height,ti.pic_x,ti.pic_y); /*{ int arg = 0xffff; th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_MBMODE,&arg,sizeof(arg)); th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_MV,&arg,sizeof(arg)); th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_QI,&arg,sizeof(arg)); arg=10; th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_BITS,&arg,sizeof(arg)); }*/ }else{ /* tear down the partial theora setup */ th_info_clear(&ti); th_comment_clear(&tc); } /*Either way, we're done with the codec setup data.*/ th_setup_free(ts); /* open video */ if(theora_p)open_video(); if(!raw && outfile){ static const char *CHROMA_TYPES[4]={"420jpeg",NULL,"422jpeg","444"}; int width; int height; if(ti.pixel_fmt>=4||ti.pixel_fmt==TH_PF_RSVD){ fprintf(stderr,"Unknown pixel format: %i\n",ti.pixel_fmt); exit(1); } if(crop){ int hdec; int vdec; hdec=!(ti.pixel_fmt&1); vdec=!(ti.pixel_fmt&2); if((ti.pic_x&hdec)||(ti.pic_width&hdec) ||(ti.pic_y&vdec)||(ti.pic_height&vdec)){ fprintf(stderr, "Error: Cropped images with odd offsets/sizes and chroma subsampling\n" "cannot be output to YUV4MPEG2. Remove the --crop flag or add the\n" "--raw flag.\n"); exit(1); } width=ti.pic_width; height=ti.pic_height; } else{ width=ti.frame_width; height=ti.frame_height; } fprintf(outfile,"YUV4MPEG2 C%s W%d H%d F%d:%d I%c A%d:%d\n", CHROMA_TYPES[ti.pixel_fmt],width,height, ti.fps_numerator,ti.fps_denominator,'p', ti.aspect_numerator,ti.aspect_denominator); } /* install signal handler */ signal (SIGINT, sigint_handler); /*Finally the main decode loop. It's one Theora packet per frame, so this is pretty straightforward if we're not trying to maintain sync with other multiplexed streams. The videobuf_ready flag is used to maintain the input buffer in the libogg stream state. If there's no output frame available at the end of the decode step, we must need more input data. We could simplify this by just using the return code on ogg_page_packetout(), but the flag system extends easily to the case where you care about more than one multiplexed stream (like with audio playback). In that case, just maintain a flag for each decoder you care about, and pull data when any one of them stalls. videobuf_time holds the presentation time of the currently buffered video frame. We ignore this value.*/ stateflag=0; /* playback has not begun */ /* queue any remaining pages from data we buffered but that did not contain headers */ while(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } if(fps_only){ ftime(&start); ftime(&last); } while(!got_sigint){ while(theora_p && !videobuf_ready){ /* theora is one in, one out... */ if(ogg_stream_packetout(&to,&op)>0){ if(th_decode_packetin(td,&op,&videobuf_granulepos)>=0){ videobuf_time=th_granule_time(td,videobuf_granulepos); videobuf_ready=1; frames++; if(fps_only) ftime(&after); } }else break; } if(fps_only && (videobuf_ready || fps_only==2)){ long ms = after.time*1000.+after.millitm- (last.time*1000.+last.millitm); if(ms>500 || fps_only==1 || (feof(infile) && !videobuf_ready)){ float file_fps = (float)ti.fps_numerator/ti.fps_denominator; fps_only=2; ms = after.time*1000.+after.millitm- (start.time*1000.+start.millitm); fprintf(stderr,"\rframe:%d rate:%.2fx ", frames, frames*1000./(ms*file_fps)); memcpy(&last,&after,sizeof(last)); } } if(!videobuf_ready && feof(infile))break; if(!videobuf_ready){ /* no data yet for somebody. Grab another page */ buffer_data(infile,&oy); while(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } } /* dumpvideo frame, and get new one */ else if(outfile)video_write(); videobuf_ready=0; } /* end of decoder loop -- close everything */ if(theora_p){ ogg_stream_clear(&to); th_decode_free(td); th_comment_clear(&tc); th_info_clear(&ti); } ogg_sync_clear(&oy); if(infile && infile!=stdin)fclose(infile); if(outfile && outfile!=stdout)fclose(outfile); fprintf(stderr, "\n\n%d frames\n", frames); fprintf(stderr, "\nDone.\n"); return(0); }
static EncoderContext *init_encoder(const char *filename, int gop_size, int width, int height, AVRational framerate, enum AVPixelFormat pix_fmt, AVDictionary *_opt) { EncoderContext *ec = (EncoderContext *)calloc(1, sizeof(EncoderContext)); int ret; AVDictionary *opt = NULL; av_dict_copy(&opt, _opt, 0); /* Allocate output context */ avformat_alloc_output_context2(&(ec->oc), NULL, NULL, filename); if (!(ec->oc)) { fprintf(stderr, "Could not deduce output format from file extension: using mp4.\n"); avformat_alloc_output_context2(&(ec->oc), NULL, "mp4", filename); if (!(ec->oc)) { fprintf(stderr, "Could not allocate output format context\n"); exit(1); } } ec->fmt = ec->oc->oformat; /* Add the video stream using the default format codecs * and initialize the codecs. */ if (ec->fmt->video_codec != AV_CODEC_ID_NONE) { add_stream(&(ec->video_st), ec->oc, &(ec->videoCodec), ec->fmt->video_codec, gop_size, width, height, framerate, pix_fmt); open_video(ec->oc, ec->videoCodec, &(ec->video_st), opt); } //av_dump_format(ec->oc, 0, filename, 1); /* open the output file, if needed */ if (!(ec->fmt->flags & AVFMT_NOFILE)) { ret = avio_open(&(ec->oc->pb), filename, AVIO_FLAG_WRITE); if (ret < 0) { fprintf(stderr, "Could not open '%s': %s\n", filename, av_err2str(ret)); exit(1); } } /* Write the stream header, if any. */ ret = avformat_write_header(ec->oc, &opt); if (ret < 0) { fprintf(stderr, "Error occurred when opening output file: %s\n", av_err2str(ret)); exit(1); } ec->endcode[0] = 0; ec->endcode[1] = 0; ec->endcode[2] = 1; ec->endcode[3] = 0xb7; av_init_packet(&(ec->pkt)); ec->pkt.data = NULL; // packet data will be allocated by the encoder ec->pkt.size = 0; ec->frame_count = 0; ec->got_output = 0; return ec; }
int main(int argc,char *argv[]){ ogg_packet op; int long_option_index; int c; FILE *infile = stdin; outfile = stdout; #ifdef _WIN32 /* We need to set stdin/stdout to binary mode on windows. */ /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); #endif /* Process option arguments. */ while((c=getopt_long(argc,argv,optstring,options,&long_option_index))!=EOF){ switch(c){ case 'o': outfile=fopen(optarg,"wb"); if(outfile==NULL){ fprintf(stderr,"Unable to open output file '%s'\n", optarg); exit(1); } break; default: usage(); } } if(optind<argc){ infile=fopen(argv[optind],"rb"); if(infile==NULL){ fprintf(stderr,"Unable to open '%s' for extraction.\n", argv[optind]); exit(1); } if(++optind<argc){ usage(); exit(1); } } /* Ok, Ogg parsing. The idea here is we have a bitstream that is made up of Ogg pages. The libogg sync layer will find them for us. There may be pages from several logical streams interleaved; we find the first theora stream and ignore any others. Then we pass the pages for our stream to the libogg stream layer which assembles our original set of packets out of them. It's the packets that libtheora actually knows how to handle. */ /* start up Ogg stream synchronization layer */ ogg_sync_init(&oy); /* init supporting Theora structures needed in header parsing */ theora_comment_init(&tc); theora_info_init(&ti); /* Ogg file open; parse the headers */ /* Vorbis and Theora both depend on some initial header packets for decoder setup and initialization. We retrieve these first before entering the main decode loop. */ /* Only interested in Theora streams */ while(!stateflag){ int ret=buffer_data(infile,&oy); if(ret==0)break; while(ogg_sync_pageout(&oy,&og)>0){ ogg_stream_state test; /* is this a mandated initial header? If not, stop parsing */ if(!ogg_page_bos(&og)){ /* don't leak the page; get it into the appropriate stream */ queue_page(&og); stateflag=1; break; } ogg_stream_init(&test,ogg_page_serialno(&og)); ogg_stream_pagein(&test,&og); ogg_stream_packetout(&test,&op); /* identify the codec: try theora */ if(!theora_p && theora_decode_header(&ti,&tc,&op)>=0){ /* it is theora -- save this stream state */ memcpy(&to,&test,sizeof(test)); theora_p=1; }else{ /* whatever it is, we don't care about it */ ogg_stream_clear(&test); } } /* fall through to non-initial page parsing */ } /* we're expecting more header packets. */ while(theora_p && theora_p<3){ int ret; /* look for further theora headers */ while(theora_p && (theora_p<3) && (ret=ogg_stream_packetout(&to,&op))){ if(ret<0){ fprintf(stderr,"Error parsing Theora stream headers; corrupt stream?\n"); exit(1); } if(theora_decode_header(&ti,&tc,&op)){ printf("Error parsing Theora stream headers; corrupt stream?\n"); exit(1); } theora_p++; if(theora_p==3)break; } /* The header pages/packets will arrive before anything else we care about, or the stream is not obeying spec */ if(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); /* demux into the stream state */ }else{ int ret=buffer_data(infile,&oy); /* need more data */ if(ret==0){ fprintf(stderr,"End of file while searching for codec headers.\n"); exit(1); } } } /* Now we have all the required headers. initialize the decoder. */ if(theora_p){ theora_decode_init(&td,&ti); fprintf(stderr,"Ogg logical stream %x is Theora %dx%d %.02f fps video\nEncoded frame content is %dx%d with %dx%d offset\n", to.serialno,ti.width,ti.height, (double)ti.fps_numerator/ti.fps_denominator, ti.frame_width, ti.frame_height, ti.offset_x, ti.offset_y); }else{ /* tear down the partial theora setup */ theora_info_clear(&ti); theora_comment_clear(&tc); } /* open video */ if(theora_p)open_video(); /* install signal handler */ signal (SIGINT, sigint_handler); /* Finally the main decode loop. It's one Theora packet per frame, so this is pretty straightforward if we're not trying to maintain sync with other multiplexed streams. the videobuf_ready flag is used to maintain the input buffer in the libogg stream state. If there's no output frame available at the end of the decode step, we must need more input data. We could simplify this by just using the return code on ogg_page_packetout(), but the flag system extends easily to the case were you care about more than one multiplexed stream (like with audio playback). In that case, just maintain a flag for each decoder you care about, and pull data when any one of them stalls. videobuf_time holds the presentation time of the currently buffered video frame. We ignore this value. */ stateflag=0; /* playback has not begun */ /* queue any remaining pages from data we buffered but that did not contain headers */ while(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } while(!got_sigint){ while(theora_p && !videobuf_ready){ /* theora is one in, one out... */ if(ogg_stream_packetout(&to,&op)>0){ theora_decode_packetin(&td,&op); videobuf_granulepos=td.granulepos; videobuf_time=theora_granule_time(&td,videobuf_granulepos); videobuf_ready=1; }else break; } if(!videobuf_ready && feof(infile))break; if(!videobuf_ready){ /* no data yet for somebody. Grab another page */ int ret=buffer_data(infile,&oy); while(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } } /* dumpvideo frame, and get new one */ else video_write(); videobuf_ready=0; } /* end of decoder loop -- close everything */ if(theora_p){ ogg_stream_clear(&to); theora_clear(&td); theora_comment_clear(&tc); theora_info_clear(&ti); } ogg_sync_clear(&oy); if(infile && infile!=stdin)fclose(infile); fprintf(stderr, "\r " "\nDone.\n"); return(0); }
void create_video_file(const char*filename,int width,int height) { /* auto detect the output format from the name. default is mpeg. */ //fmt = av_guess_format(NULL, filename, NULL); #if (LIBAVFORMAT_VERSION_INT>=AV_VERSION_INT(52,81,0)) #define libavformat_guess_format av_guess_format #else #define libavformat_guess_format guess_format #endif fmt = libavformat_guess_format(NULL, filename, NULL); if (!fmt) { printf("Could not deduce output format from file extension: using MPEG.\n"); //fmt = av_guess_format("mpeg", NULL, NULL); fmt = libavformat_guess_format("mpeg", NULL, NULL); } if (!fmt) { fprintf(stderr, "Could not find suitable output format\n"); exit(1); } /* allocate the output media context */ oc = avformat_alloc_context(); if (!oc) { fprintf(stderr, "Memory error\n"); exit(1); } oc->oformat = fmt; snprintf(oc->filename, sizeof(oc->filename), "%s", filename); /* add the audio and video streams using the default format codecs and initialize the codecs */ video_st = NULL; if (fmt->video_codec != CODEC_ID_NONE) { video_st = add_video_stream(oc, fmt->video_codec,width,height); } /* set the output parameters (must be done even if no parameters). */ if (av_set_parameters(oc, NULL) < 0) { fprintf(stderr, "Invalid output format parameters\n"); exit(1); } dump_format(oc, 0, filename, 1); /* now that all the parameters are set, we can open the audio and video codecs and allocate the necessary encode buffers */ if (video_st) open_video(oc, video_st); /* open the output file, if needed */ if (!(fmt->flags & AVFMT_NOFILE)) { if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) { fprintf(stderr, "Could not open '%s'\n", filename); exit(1); } } /* write the stream header, if any */ av_write_header(oc); }
bool CTheoraPlayer::playVideo( //Plays specified OGG Theora file to screen surface. //If screen == NULL, then this method will test that the file is playable //by decoding it as fast as possible but not displaying anything. // //Returns: whether playback was successful CStretchyBuffer& buffer, SDL_Surface *screen, const int x, const int y) //[default=(0,0)] { //init theora_p = vorbis_p = 0; startticks = 0; bool bSkippedLastFrame = false; // start up Ogg stream synchronization layer ogg_sync_init(&oy); // init supporting Vorbis structures needed in header parsing vorbis_info_init(&vi); vorbis_comment_init(&vc); // init supporting Theora structures needed in header parsing theora_comment_init(&tc); theora_info_init(&ti); if (!screen) ti.quick_p = 1; ti.quality = 63; if (!parseHeaders(buffer)) return false; // force audio off vorbis_p = 0; // initialize decoders if (theora_p) { theora_decode_init(&td,&ti); #if 0 printf("Ogg logical stream %x is Theora %dx%d %.02f fps video\n" " Frame content is %dx%d with offset (%d,%d).\n", to.serialno,ti.width,ti.height, (double)ti.fps_numerator/ti.fps_denominator, ti.frame_width, ti.frame_height, ti.offset_x, ti.offset_y); //report_colorspace(&ti); //we're not using this info for anything dump_comments(&tc); #endif } else { // tear down the partial theora setup theora_info_clear(&ti); theora_comment_clear(&tc); } if(vorbis_p) { vorbis_synthesis_init(&vd,&vi); vorbis_block_init(&vd,&vb); printf("Ogg logical stream %lx is Vorbis %d channel %ld Hz audio.\n", vo.serialno,vi.channels,vi.rate); } else { // tear down the partial vorbis setup vorbis_info_clear(&vi); vorbis_comment_clear(&vc); } // open audio if (vorbis_p) open_audio(); // open video SDL_Overlay *yuv_overlay = NULL; if (theora_p && screen) yuv_overlay = open_video(screen); // single frame video buffering ogg_packet op; ogg_int64_t videobuf_granulepos=-1; double videobuf_time=0; double last_frame_time = 0; bool hasdatatobuffer = true; // Main loop bool audiobuf_ready=false; bool videobuf_ready=false; bool playbackdone = (yuv_overlay == NULL); bool isPlaying = false; bool bBreakout = false; while (!playbackdone) { // break out on SDL quit event SDL_Event event; if (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: playbackdone = bBreakout = true; break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) playbackdone = bBreakout = true; break; default: break; } } while (theora_p && !videobuf_ready) { // get one video packet... if (ogg_stream_packetout(&to,&op)>0) { theora_decode_packetin(&td,&op); videobuf_granulepos=td.granulepos; videobuf_time=theora_granule_time(&td,videobuf_granulepos); #if 0 //Without sound channels to synch to, don't need to worry about skipping frames when slow. // update the frame counter //++frameNum; // check if this frame time has not passed yet. // If the frame is late we need to decode additional // ones and keep looping, since theora at this stage // needs to decode all frames. const double now=get_time(); const double delay=videobuf_time-now; if(delay>=0.0){ /// got a good frame, not late, ready to break out videobuf_ready=true; } else if(now-last_frame_time>=1.0) { // display at least one frame per second, regardless videobuf_ready=true; } else { //Need to catch up -- no time to display frame. if (bSkippedLastFrame) //only allow skipping one frame in a row videobuf_ready = true; //show anyway else bSkippedLastFrame = true; //printf("dropping frame %d (%.3fs behind)\n", frameNum, -delay); } #else videobuf_ready = true; //show every frame #endif } else { // need more data break; } } if (!hasdatatobuffer && !videobuf_ready && !audiobuf_ready) { isPlaying = false; playbackdone = true; } //If we're set for the next frame, sleep. //In other words, don't show frames too rapidly. if((!theora_p || videobuf_ready) && (!vorbis_p || audiobuf_ready)) { const int ticks = (int)(1000*(videobuf_time-get_time())); if(ticks>0 && screen) //don't need to sleep if only testing file SDL_Delay(ticks); } if (videobuf_ready) { // time to write our cached frame if (screen) { const bool bRes = video_write(screen, yuv_overlay, x, y); if (!bRes) //couldn't display image playbackdone = bBreakout = true; } videobuf_ready=false; last_frame_time=get_time(); bSkippedLastFrame = false; // if audio has not started (first frame) then start it if ((!isPlaying)&&(vorbis_p)) { start_audio(); isPlaying = true; } } // HACK: always look for more audio data audiobuf_ready=false; // buffer compressed data every loop if (hasdatatobuffer) { hasdatatobuffer = buffer_data(&oy, buffer) > 0; if (!hasdatatobuffer) { //printf("Ogg buffering stopped, end of file reached.\n"); } } if (ogg_sync_pageout(&oy,&og)>0) queue_page(&og); } // playbackdone // show number of video frames decoded //printf("\nFrames decoded: %d\n", frameNum); // deinit if (vorbis_p) { audio_close(); ogg_stream_clear(&vo); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_comment_clear(&vc); vorbis_info_clear(&vi); } if (theora_p) { if (yuv_overlay) SDL_FreeYUVOverlay(yuv_overlay); ogg_stream_clear(&to); theora_clear(&td); theora_comment_clear(&tc); theora_info_clear(&ti); } ogg_sync_clear(&oy); //If broken out of testing, return false since entire file was not verified. return !bBreakout || screen != NULL; }
bool FFMPEGer::init(MetaData* meta){ if(mInited) return true; do{ AVDictionary *opt = NULL; int ret; av_register_all(); avformat_alloc_output_context2(&fmt_ctx, NULL, NULL, mOutputFile); if(fmt_ctx == NULL){ ALOGE("fail to avformat_alloc_output_context2 for %s", mOutputFile); break; } fmt = fmt_ctx->oformat; /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ if (fmt->video_codec != AV_CODEC_ID_NONE) { add_stream(&video_st, fmt_ctx, &video_codec, fmt->video_codec); have_video = true; } if (fmt->audio_codec != AV_CODEC_ID_NONE) { add_stream(&audio_st, fmt_ctx, &audio_codec, fmt->audio_codec); have_audio = true; } if(!have_audio && !have_video){ ALOGE("no audio or video codec found for the fmt!"); break; } /* Now that all the parameters are set, we can open the audio and * video codecs and allocate the necessary encode buffers. */ if (have_video) open_video(video_codec, &video_st, opt); if (have_audio) open_audio(audio_codec, &audio_st, opt); /* open the output file, if needed */ if (!(fmt->flags & AVFMT_NOFILE)) { ret = avio_open(&fmt_ctx->pb, mOutputFile, AVIO_FLAG_WRITE); if (ret < 0) { ALOGE("Could not open '%s': %s", mOutputFile, av_err2str(ret)); break; } } /* Write the stream header, if any. */ ret = avformat_write_header(fmt_ctx, NULL); if (ret < 0) { ALOGE("Error occurred when opening output file: %s", av_err2str(ret)); break; } mInited = true; }while(0); if(!mInited) reset(); return mInited; }
void GLWindow::openVideo() { cv::VideoCapture video(QFileDialog::getOpenFileName(this, "Open Video").toStdString()); open_video(video, "D:/vids/out.avi"); }
void GLWindow::openGoodEats() { cv::VideoCapture video("D:/vids/Good Eats/Couch Potato Weekend (Potato Interstitials)/Couch Potato Weekend - A Spud In Every Tank.mp4"); open_video(video, "D:/vids/good_eats.avi"); }
int main(int argc, char ** argv) { if(argc < 4) { printf("\nScrub, you need to specify a bitrate, number of frames, and server." "\nLike this: pixieHD 350 1000 rtmp://domain.com/live/matt\n" "\nNOTE, it is: progname bitrate frames server\n\n" "The bitrate is understood to be kbits/sec.\n" "You should enter frames or else you the program will\n" "continue to stream until you forcefully close it.\n" "THANK YOU: while(1) { /* stream! */ }\n"); return 0; } printf("\nYou have set the following options:\n\n%5cbitrate: %s," "\n%5cframes: %s\n%5cserver: %s\n\n", ' ',argv[1],' ',argv[2],' ',argv[3]); /*int p; printf("Initializing noob options"); for(p=0; p<3; ++p) { printf("%5c",'.'); Sleep(1500); } printf("\n\n"); char *input; printf("You hating on my GFX or wat? Please Answer: "); input = getline(); printf("\n\n"); printf("Your answer: "); size_t input_len = strlen(input); for(p=0; p<input_len; ++p) { Sleep(300); printf("%c",input[p]); } printf("\nkk here we go..."); Sleep(1000);*/ printf("\n\nPress the CONTROL key to begin streaming or ESC key to QUIT.\n\n"); while (1) { if (ButtonPress(VK_ESCAPE)) { printf("Quit.\n\n"); break; } else if (ButtonPress(VK_CONTROL)) { // Decoder local variable declaration AVFormatContext *pFormatCtx = NULL; int i, videoStream; AVCodecContext *pCodecCtx = NULL; AVCodec *pCodec; AVFrame *pFrame; AVPacket packet; int frameFinished; // Encoder local variable declaration const char *filename; AVOutputFormat *fmt; AVFormatContext *oc; AVStream *video_st; AVCodec *video_codec; int ret; unsigned int frame_count, frame_count2; StreamInfo sInfo; size_t max_frames = strtol(argv[2], NULL, 0); // Register all formats, codecs and network av_register_all(); avcodec_register_all(); avformat_network_init(); // Setup mux //filename = "output_file.flv"; //filename = "rtmp://chineseforall.org/live/beta"; filename = argv[3]; fmt = av_guess_format("flv", filename, NULL); if (fmt == NULL) { printf("Could not guess format.\n"); return -1; } // allocate the output media context oc = avformat_alloc_context(); if (oc == NULL) { printf("could not allocate context.\n"); return -1; } HDC hScreen = GetDC(GetDesktopWindow()); ScreenX = GetDeviceCaps(hScreen, HORZRES); ScreenY = GetDeviceCaps(hScreen, VERTRES); // Temp. hard-code the resolution int new_width = 1024, new_height = 576; double v_ratio = 1.7786458333333333333333333333333; // Set output format context to the format ffmpeg guessed oc->oformat = fmt; // Add the video stream using the h.264 // codec and initialize the codec. video_st = NULL; sInfo.width = new_width; sInfo.height = new_height; sInfo.pix_fmt = AV_PIX_FMT_YUV420P; sInfo.frame_rate = 10; sInfo.bitrate = strtol(argv[1], NULL, 0)*1000; video_st = add_stream(oc, &video_codec, AV_CODEC_ID_H264, &sInfo); // Now that all the parameters are set, we can open the audio and // video codecs and allocate the necessary encode buffers. if (video_st) open_video(oc, video_codec, video_st); /* open the output file, if needed */ if (!(fmt->flags & AVFMT_NOFILE)) { ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE); if (ret < 0) { fprintf(stderr, "Could not open '%s': %s\n", filename, av_err2str(ret)); return 1; } } // dump output format av_dump_format(oc, 0, filename, 1); // Write the stream header, if any. ret = avformat_write_header(oc, NULL); if (ret < 0) { fprintf(stderr, "Error occurred when opening output file: %s\n", av_err2str(ret)); return 1; } // Read frames, decode, and re-encode frame_count = 1; frame_count2 = 1; HDC hdcMem = CreateCompatibleDC (hScreen); HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, ScreenX, ScreenY); HGDIOBJ hOld; BITMAPINFOHEADER bmi = {0}; bmi.biSize = sizeof(BITMAPINFOHEADER); bmi.biPlanes = 1; bmi.biBitCount = 32; bmi.biWidth = ScreenX; bmi.biHeight = -ScreenY; bmi.biCompression = BI_RGB; bmi.biSizeImage = 0;// 3 * ScreenX * ScreenY; if(ScreenData) free(ScreenData); ScreenData = (BYTE*)malloc(4 * ScreenX * ScreenY); AVPacket pkt; clock_t start_t = GetTickCount(); long long wait_time = 0; uint64_t total_size; while(1) { hOld = SelectObject(hdcMem, hBitmap); BitBlt(hdcMem, 0, 0, ScreenX, ScreenY, hScreen, 0, 0, SRCCOPY); SelectObject(hdcMem, hOld); GetDIBits(hdcMem, hBitmap, 0, ScreenY, ScreenData, (BITMAPINFO*)&bmi, DIB_RGB_COLORS); //calculate the bytes needed for the output image int nbytes = avpicture_get_size(AV_PIX_FMT_YUV420P, new_width, new_height); //create buffer for the output image uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes); //create ffmpeg frame structures. These do not allocate space for image data, //just the pointers and other information about the image. AVFrame* inpic = avcodec_alloc_frame(); AVFrame* outpic = avcodec_alloc_frame(); //this will set the pointers in the frame structures to the right points in //the input and output buffers. avpicture_fill((AVPicture*)inpic, ScreenData, AV_PIX_FMT_RGB32, ScreenX, ScreenY); avpicture_fill((AVPicture*)outpic, outbuffer, AV_PIX_FMT_YUV420P, new_width, new_height); //create the conversion context struct SwsContext *fooContext = sws_getContext(ScreenX, ScreenY, AV_PIX_FMT_RGB32, new_width, new_height, AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL); //perform the conversion sws_scale(fooContext, inpic->data, inpic->linesize, 0, ScreenY, outpic->data, outpic->linesize); // Initialize a new frame AVFrame* newFrame = avcodec_alloc_frame(); int size = avpicture_get_size(video_st->codec->pix_fmt, video_st->codec->width, video_st->codec->height); uint8_t* picture_buf = av_malloc(size); avpicture_fill((AVPicture *) newFrame, picture_buf, video_st->codec->pix_fmt, video_st->codec->width, video_st->codec->height); // Copy only the frame content without additional fields av_picture_copy((AVPicture*) newFrame, (AVPicture*) outpic, video_st->codec->pix_fmt, video_st->codec->width, video_st->codec->height); // encode the image int got_output; av_init_packet(&pkt); pkt.data = NULL; // packet data will be allocated by the encoder pkt.size = 0; // Set the frame's pts (this prevents the warning notice 'non-strictly-monotonic PTS') newFrame->pts = frame_count2; ret = avcodec_encode_video2(video_st->codec, &pkt, newFrame, &got_output); if (ret < 0) { fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret)); exit(1); } if (got_output) { if (video_st->codec->coded_frame->key_frame) pkt.flags |= AV_PKT_FLAG_KEY; pkt.stream_index = video_st->index; if (pkt.pts != AV_NOPTS_VALUE) pkt.pts = av_rescale_q(pkt.pts, video_st->codec->time_base, video_st->time_base); if (pkt.dts != AV_NOPTS_VALUE) pkt.dts = av_rescale_q(pkt.dts, video_st->codec->time_base, video_st->time_base); // Write the compressed frame to the media file. ret = av_interleaved_write_frame(oc, &pkt); fprintf(stderr, "encoded frame #%d\n", frame_count); frame_count++; } else { ret = 0; } if (ret != 0) { fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret)); exit(1); } ++frame_count2; // Free the YUV picture frame we copied from the // decoder to eliminate the additional fields // and other packets/frames used av_free(picture_buf); av_free_packet(&pkt); av_free(newFrame); //free memory av_free(outbuffer); av_free(inpic); av_free(outpic); if(frame_count == max_frames) { /* Write the trailer, if any. The trailer must be written before you * close the CodecContexts open when you wrote the header; otherwise * av_write_trailer() may try to use memory that was freed on * av_codec_close(). */ av_write_trailer(oc); /* Close the video codec (encoder) */ if (video_st) { close_video(oc, video_st); } // Free the output streams. for (i = 0; i < oc->nb_streams; i++) { av_freep(&oc->streams[i]->codec); av_freep(&oc->streams[i]); } if (!(fmt->flags & AVFMT_NOFILE)) { /* Close the output file. */ avio_close(oc->pb); } /* free the output format context */ av_free(oc); ReleaseDC(GetDesktopWindow(),hScreen); DeleteDC(hdcMem); printf("\n\nPress the CONTROL key to begin streaming or ESC key to QUIT.\n\n"); break; } } } } return 0; }
void GLWindow::openGoT() { cv::VideoCapture video("D:/vids/got_short.mp4"); open_video(video, "D:/vids/got.avi"); }
int main(int argc, char **argv) { const char *filename; AVOutputFormat *fmt; AVFormatContext *oc; AVStream *audio_st, *video_st; AVCodec *audio_codec, *video_codec; double audio_time, video_time; int flush, ret; /* Initialize libavcodec, and register all codecs and formats. */ av_register_all(); if (argc != 2) { printf("usage: %s output_file\n" "API example program to output a media file with libavformat.\n" "This program generates a synthetic audio and video stream, encodes and\n" "muxes them into a file named output_file.\n" "The output format is automatically guessed according to the file extension.\n" "Raw images can also be output by using '%%d' in the filename.\n" "\n", argv[0]); return 1; } filename = argv[1]; /* allocate the output media context */ avformat_alloc_output_context2(&oc, NULL, NULL, filename); if (!oc) { printf("Could not deduce output format from file extension: using MPEG.\n"); avformat_alloc_output_context2(&oc, NULL, "mpeg", filename); } if (!oc) return 1; fmt = oc->oformat; /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ video_st = NULL; audio_st = NULL; if (fmt->video_codec != AV_CODEC_ID_NONE) video_st = add_stream(oc, &video_codec, fmt->video_codec); if (fmt->audio_codec != AV_CODEC_ID_NONE) audio_st = add_stream(oc, &audio_codec, fmt->audio_codec); /* Now that all the parameters are set, we can open the audio and * video codecs and allocate the necessary encode buffers. */ if (video_st) open_video(oc, video_codec, video_st); if (audio_st) open_audio(oc, audio_codec, audio_st); av_dump_format(oc, 0, filename, 1); /* open the output file, if needed */ if (!(fmt->flags & AVFMT_NOFILE)) { ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE); if (ret < 0) { fprintf(stderr, "Could not open '%s': %s\n", filename, av_err2str(ret)); return 1; } } /* Write the stream header, if any. */ ret = avformat_write_header(oc, NULL); if (ret < 0) { fprintf(stderr, "Error occurred when opening output file: %s\n", av_err2str(ret)); return 1; } flush = 0; while ((video_st && !video_is_eof) || (audio_st && !audio_is_eof)) { /* Compute current audio and video time. */ audio_time = (audio_st && !audio_is_eof) ? audio_st->pts.val * av_q2d(audio_st->time_base) : INFINITY; video_time = (video_st && !video_is_eof) ? video_st->pts.val * av_q2d(video_st->time_base) : INFINITY; if (!flush && (!audio_st || audio_time >= STREAM_DURATION) && (!video_st || video_time >= STREAM_DURATION)) { flush = 1; } /* write interleaved audio and video frames */ if (audio_st && !audio_is_eof && audio_time <= video_time) { write_audio_frame(oc, audio_st, flush); } else if (video_st && !video_is_eof && video_time < audio_time) { write_video_frame(oc, video_st, flush); } } /* Write the trailer, if any. The trailer must be written before you * close the CodecContexts open when you wrote the header; otherwise * av_write_trailer() may try to use memory that was freed on * av_codec_close(). */ av_write_trailer(oc); /* Close each codec. */ if (video_st) close_video(oc, video_st); if (audio_st) close_audio(oc, audio_st); if (!(fmt->flags & AVFMT_NOFILE)) /* Close the output file. */ avio_close(oc->pb); /* free the stream */ avformat_free_context(oc); return 0; }
void GLWindow::openWebcam() { cv::VideoCapture cam(0); open_video(cam, "D:/vids/cam.avi"); }
/* * ����һ�������������ģ�����Ƶ�ļ����н������ */ AVDecodeCtx *ffCreateDecodeContext( const char * filename, AVDictionary *opt_arg ) { int i, ret; AVInputFormat *file_iformat = NULL; AVDecodeCtx * pdc; AVDictionary * opt = NULL; ffInit(); pdc = (AVDecodeCtx *)malloc(sizeof(AVDecodeCtx)); while (pdc) { memset(pdc, 0, sizeof(AVDecodeCtx)); pdc->_fileName = strdup(filename); pdc->_ctx = avformat_alloc_context(); if (!pdc->_ctx) { av_log(NULL, AV_LOG_FATAL, "ffCreateDecodeContext : could not allocate context.\n"); break; } //filename = "video=.." ,open dshow device if (filename && strstr(filename, "video=") == filename){ file_iformat = av_find_input_format(CAP_DEVICE_NAME); if (!file_iformat){ av_log(NULL, AV_LOG_FATAL, "Unknown input format: '%s'\n",CAP_DEVICE_NAME); break; } } av_dict_copy(&opt, opt_arg, 0); ret = avformat_open_input(&pdc->_ctx, filename, file_iformat, &opt); av_dict_free(&opt); opt = NULL; if (ret < 0) { char errmsg[ERROR_BUFFER_SIZE]; av_strerror(ret, errmsg, ERROR_BUFFER_SIZE); av_log(NULL, AV_LOG_FATAL, "ffCreateDecodeContext %s.\n", errmsg); break; } av_format_inject_global_side_data(pdc->_ctx); av_dict_copy(&opt, opt_arg, 0); ret = avformat_find_stream_info(pdc->_ctx, NULL); av_dict_free(&opt); opt = NULL; if (ret < 0) { char errmsg[ERROR_BUFFER_SIZE]; av_strerror(ret, errmsg, ERROR_BUFFER_SIZE); av_log(NULL, AV_LOG_FATAL, "ffCreateDecodeContext %s.\n", errmsg); break; } /* * ������Ƶ������Ƶ�� */ ret = av_find_best_stream(pdc->_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); if (ret >= 0) { pdc->has_video = 1; pdc->_video_st = pdc->_ctx->streams[ret]; pdc->_video_st_index = ret; } ret = av_find_best_stream(pdc->_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0); if (ret >= 0) { pdc->has_audio = 1; pdc->_audio_st = pdc->_ctx->streams[ret]; pdc->_audio_st_index = ret; } if (pdc->has_video) { if (open_video(pdc, pdc->_video_st->codec->codec_id, NULL) < 0) { ffCloseDecodeContext(pdc); return NULL; } pdc->encode_video = 1; } if (pdc->has_audio) { if (open_audio(pdc, pdc->_audio_st->codec->codec_id, NULL) < 0) { ffCloseDecodeContext(pdc); return NULL; } pdc->encode_audio = 1; } return pdc; } /* * ʧ������ */ ffCloseDecodeContext(pdc); return NULL; }