2014-04-14 5 views
1

, 내가 ... 내가했던 가장 먼저 MacPorts를 사용하고는 FFmpeg 및 SDL을 얻을 수 있었다, 그래서 맥이내가 (학교) drangers 가이드로부터는 FFmpeg을 배우려고 노력하고있어

이제는 컴파일러에서 헤더를 인식하지 못했습니다 ... 그래서 컴파일시 gnu를 사용하고 헤더에 "전체/경로/이름 ..."을 부여했습니다. 하지만 필자는 누락 된 일부 헤더에 항상 오류가 발생합니다 ...

아래 코드는 수정되었습니다.

이 나는 ​​또한에 대한 오류를 얻을 수 있지만, 컴파일러는 내가 (콘솔)와 엑스 코드에 모두 GNU 시도

찾을 수있는 또 다른 헤더는 항상 존재하는 모든 헤더를 포함했습니다.

// tutorial01.c 
// Code based on a tutorial by Martin Bohme ([email protected]) 
// Tested on Gentoo, CVS version 5/01/07 compiled with GCC 4.1.1 

// A small sample program that shows how to use libavformat and libavcodec to 
// read video from a file. 
// 
// Use 
// 
// gcc -o tutorial01 tutorial01.c -lavformat -lavcodec -lz 
// 
// to build (assuming libavformat and libavcodec are correctly installed 
// your system). 
// 
// Run using 
// 
// tutorial01 myvideofile.mpg 
// 
// to write the first five frames from "myvideofile.mpg" to disk in PPM 
// format. 

#include "/opt/local/include/libavcodec/avcodec.h" 
#include "/opt/local/include/libavformat/avformat.h" 

#include <stdio.h> 

void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) { 
    FILE *pFile; 
    char szFilename[32]; 
    int y; 

    // Open file 
    sprintf(szFilename, "frame%d.ppm", iFrame); 
    pFile=fopen(szFilename, "wb"); 
    if(pFile==NULL) 
    return; 

    // Write header 
    fprintf(pFile, "P6\n%d %d\n255\n", width, height); 

    // Write pixel data 
    for(y=0; y<height; y++) 
    fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile); 

    // Close file 
    fclose(pFile); 
} 

int main(int argc, char *argv[]) { 
    AVFormatContext *pFormatCtx; 
    int    i, videoStream; 
    AVCodecContext *pCodecCtx; 
    AVCodec   *pCodec; 
    AVFrame   *pFrame; 
    AVFrame   *pFrameRGB; 
    AVPacket  packet; 
    int    frameFinished; 
    int    numBytes; 
    uint8_t   *buffer; 

    if(argc < 2) { 
    printf("Please provide a movie file\n"); 
    return -1; 
    } 
    // Register all formats and codecs 
    av_register_all(); 

    // Open video file 
    if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0) 
    return -1; // Couldn't open file 

    // Retrieve stream information 
    if(av_find_stream_info(pFormatCtx)<0) 
    return -1; // Couldn't find stream information 

    // Dump information about file onto standard error 
    dump_format(pFormatCtx, 0, argv[1], 0); 

    // Find the first video stream 
    videoStream=-1; 
    for(i=0; i<pFormatCtx->nb_streams; i++) 
    if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) { 
     videoStream=i; 
     break; 
    } 
    if(videoStream==-1) 
    return -1; // Didn't find a video stream 

    // Get a pointer to the codec context for the video stream 
    pCodecCtx=pFormatCtx->streams[videoStream]->codec; 

    // Find the decoder for the video stream 
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id); 
    if(pCodec==NULL) { 
    fprintf(stderr, "Unsupported codec!\n"); 
    return -1; // Codec not found 
    } 
    // Open codec 
    if(avcodec_open(pCodecCtx, pCodec)<0) 
    return -1; // Could not open codec 

    // Allocate video frame 
    pFrame=avcodec_alloc_frame(); 

    // Allocate an AVFrame structure 
    pFrameRGB=avcodec_alloc_frame(); 
    if(pFrameRGB==NULL) 
    return -1; 

    // Determine required buffer size and allocate buffer 
    numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, 
        pCodecCtx->height); 
    buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t)); 

    // Assign appropriate parts of buffer to image planes in pFrameRGB 
    // Note that pFrameRGB is an AVFrame, but AVFrame is a superset 
    // of AVPicture 
    avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, 
     pCodecCtx->width, pCodecCtx->height); 

    // Read frames and save first five frames to disk 
    i=0; 
    while(av_read_frame(pFormatCtx, &packet)>=0) { 
    // Is this a packet from the video stream? 
    if(packet.stream_index==videoStream) { 
     // Decode video frame 
     avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, 
       packet.data, packet.size); 

     // Did we get a video frame? 
     if(frameFinished) { 
    // Convert the image from its native format to RGB 
    img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, 
        (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width, 
        pCodecCtx->height); 

    // Save the frame to disk 
    if(++i<=5) 
     SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, 
      i); 
     } 
    } 

    // Free the packet that was allocated by av_read_frame 
    av_free_packet(&packet); 
    } 

    // Free the RGB image 
    av_free(buffer); 
    av_free(pFrameRGB); 

    // Free the YUV frame 
    av_free(pFrame); 

    // Close the codec 
    avcodec_close(pCodecCtx); 

    // Close the video file 
    av_close_input_file(pFormatCtx); 

    return 0; 
} 
+0

이것은 내가 얻는 오류입니다. tutorial01.c에서 포함 된 파일에서 : 1 : /opt/local/include/libavcodec/avcodec.h:31:10 : 치명적인 오류 : 'libavutil/samplefmt.h'파일 찾을 수 없습니다. #include "libavutil/samplefmt.h" – orpgol

+0

https://trac.ffmpeg.org/wiki/Using%20libav* 유용 할 수 있습니다 [또한 포함 주변에 외부 C {} 블록이 필요합니다.] – rogerdpack

+0

이유는 무엇입니까? 이 태그가 C++? 이것은 C 라이브러리에 링크 된 C 코드입니다. – crashmstr

답변

0

간단 같이 포함로 이동 :

#include "avcodec.h" 

(가)와 같은 표기법을 사용하여 디렉토리를 포함 추가 컴파일러, GCC 또는 그 소리 또는 어떤 컴파일러를 사용하여 :

gcc tutorial01.c -o tut -I/opt/local/include -lavformat -lavcodec -lz -lavutil -lm 

(I를 인정하지만, 올바른 -l 매개 변수인지는 모르지만 일반적인 원칙이 적용됩니다. 포함 디렉토리 경로를 추가하려면

-I/path/to/dir
을 사용하십시오.

등등.

+2

-lavcodec가 아닌 -libavcodec이어야합니다. 또한 ffmpeg의 경우 일반적으로 "# libavccodec/avcodec.h"규칙이 사용됩니다. 그럼 당신은 단 하나의 라이브러리 대신에 하나의 -I/opt/local/include가 필요합니다. (ffmpeg에는 12 개의 라이브러리가 있습니다.) – szatmary

+0

괜찮습니다. 컴파일을 시작하고 경고 만받습니다. (주로 deprecated를 사용합니다.)하지만 이제는 링커를 얻습니다. 오류 : 오류 : -lavformat 그 소리에 대한 발견 라이브러리하지 : LD GCC tutorial01.c -o : 링커 명령은 종료 코드 1 (호출을 볼 수 -v 사용) 이 내가하는 방식으로 컴파일하는 방법입니다 함께 실패 tut -I/opt/local/include -lavformat -lavcodec -lz -lavutil -lm 또는 : $ gcc -o tutorial01 tutorial01.c -lavformat -lavcodec -lz -lavutil -lm -I/opt/local/include – orpgol

+0

'LD_LIBRARY_PATH' 환경 변수에 ffmpeg 라이브러리가 설치된 경로가 포함되어 있는지 확인하십시오. ffm도 설치 했습니까? 정적 라이브러리 대신 공유 라이브러리를 사용합니까? 공유 라이브러리와 함께 설치하려면 ffmpeg를 빌드하고 설치할 때'configure' 프로그램에 다음 두 옵션을 추가해야합니다 :'./configure --disable-static --enable-shared' – JohnH