#pragma comment(lib, "avutil.lib" )

#pragma comment(lib, "avcodec.lib" )

#pragma comment(lib, "avformat.lib" )

#pragma comment(lib, "swscale.lib" )

#pragma comment(lib, "swresample.lib" )

#pragma comment(lib, "postproc.lib" )

#pragma comment(lib, "avfilter.lib" )

extern "C" {

#include <libavformat/avformat.h>

#include <libavcodec/avcodec.h>

#include <libswresample/swresample.h>

#include <libswscale/swscale.h>

#include <libavutil/opt.h>
}

#include <iostream>

int main()
{
av_log_set_level(AV_LOG_DEBUG);
av_log(NULL, AV_LOG_INFO, "데이터를 추출할 동영상 파일의 경로를 입력하십시오.\n");

AVFormatContext* context = NULL;

char input_data[300];
std::cin.getline(input_data, 300, '\n');
int ret = avformat_open_input(&context, input_data, NULL, NULL);

while (ret != 0)
{
    av_log(NULL, AV_LOG_ERROR, "해당 경로에 파일이 존재하지 않습니다. 다시 입력해주세요.\n");
    std::cin.getline(input_data, 300, '\n');
    ret = avformat_open_input(&context, input_data, NULL, NULL);

}

ret = avformat_find_stream_info(context, NULL);
if (ret < 0) {
    av_log(NULL, AV_LOG_ERROR, "Fail to get Stream Inform\n");
    exit(-1);
}

AVCodec* vCodec, * aCodec;
AVCodecContext* vCodecContext, * aCodecContext;
int vIndex = av_find_best_stream(context, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
int aIndex = av_find_best_stream(context, AVMEDIA_TYPE_AUDIO, -1, vIndex, NULL, 0);

if (vIndex >= 0) {
    vCodec = avcodec_find_decoder(context->streams[vIndex]->codecpar->codec_id);
    if (vCodec == NULL)
    {
        av_log(NULL, AV_LOG_ERROR, "avcodec_find_decoder(vcodec) failed.\n");
        exit(-1);
    }

    vCodecContext = avcodec_alloc_context3(vCodec);
    if (avcodec_open2(vCodecContext, vCodec, NULL) < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avcodec_open2(vcodec) failed.\n");
        exit(-1);
    }
}

if (aIndex >= 0) {
    aCodec = avcodec_find_decoder(context->streams[aIndex]->codecpar->codec_id);
    if (aCodec == NULL)
    {
        av_log(NULL, AV_LOG_ERROR, "avcodec_find_decoder(acodec) failed.\n");
        exit(-1);
    }
    aCodecContext = avcodec_alloc_context3(aCodec);

    if (avcodec_open2(aCodecContext, aCodec, NULL) < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avcodec_open2(acodec) failed.\n");
        exit(-1);
    }
}

avformat_close_input(&context);
system("pause");
return 0;

}