Let avformat_open_input and avformat_close_input (de)allocate the format context

Thanks to Michael Bradshaw for his advices.
Forgot to deallocate the codec context

Thanks to Michael Bradshaw for pointing that out.
Forgot to free AVPacket after each call to av_read_frame

Thanks to Michael Bradshaw for pointed that out.
Fixed strange segmentation fault by printing something
Use av_find_best_stream to find the video stream

Thanks to Michael Bradshaw for his advices