[Libav-user] Using FFmpeg programmatically with a proxy

William Seemann wseemann at gmail.com
Fri May 3 06:31:54 CEST 2013


I'm trying to use FFmpeg with a proxy. The following code works if I 
comment out:

     setenv("http_proxy", "http://172.0.0.1:3128/", 1);

The proxy server is an instance of Squid]. I know the proxy server is 
working because I can use the same URL with VLC without any issues. With 
the proxy specified, the output from my code is always:

     Proxy path: http://172.0.0.1:3128/
     Metadata could not be retrieved -5 // indicates an IO Error
     setDataSource failed, rc is: -1

I compiled FFmpeg with everything enabled. Can someone explain why this 
code doesn't work with a proxy?

     #include <stdio.h>
     #include <stdlib.h>
     #include <libavcodec/avcodec.h>
     #include <libavformat/avformat.h>

     const int SUCCESS = 0;
     const int FAILURE = -1;

     typedef struct State {
         AVFormatContext *pFormatCtx;
         int             audio_stream;
         int             video_stream;
         AVStream        *audio_st;
         AVStream        *video_st;
     } State;

     int setDataSource(State** ps, const char* path) {
         State *state = *ps;

         if (state->pFormatCtx) {
             avformat_close_input(&state->pFormatCtx);
         }

         int ret = 0;

         if ((ret = avformat_open_input(&state->pFormatCtx, path, NULL, 
NULL)) != 0) {
             printf("Metadata could not be retrieved %d\n", ret);
             *ps = NULL;
             return FAILURE;
         }

         if (avformat_find_stream_info(state->pFormatCtx, NULL) < 0) {
             printf("Metadata could not be retrieved\n");
             avformat_close_input(&state->pFormatCtx);
             *ps = NULL;
             return FAILURE;
         }

         *ps = state;
         return SUCCESS;
     }

     const char* extractMetadata(State** ps, const char* key) {
         char* value = NULL;

         State *state = *ps;

         if (!state->pFormatCtx) {
             goto fail;
         }

         if (key) {
             if (av_dict_get(state->pFormatCtx->metadata, key, NULL, 
AV_DICT_IGNORE_SUFFIX)) {
                 value = av_dict_get(state->pFormatCtx->metadata, key, 
NULL, AV_DICT_IGNORE_SUFFIX)->value;
             }
         }

         fail:

         return value;
     }

     int main(void) {
         setenv("http_proxy", "http://172.0.0.1:3128/", 1);
         unsetenv("no_proxy");

         const char *proxy_path = getenv("http_proxy");

         if (proxy_path) {
             printf("Proxy path: %s\n", proxy_path);
         } else {
             printf("No proxy specified\n");
         }

         av_register_all();
         avformat_network_init();

         State * state = av_mallocz(sizeof(State));

         int ret = setDataSource(&state, "http://<some host>/song.mp3");

         if (ret == 0) {
             printf("setDataSource succedded!\n");
             printf("Artist: %s\n", extractMetadata(&state, "artist"));
         } else {
             printf("setDataSource failed, rc is: %d\n", ret);
         }

         return EXIT_SUCCESS;
     }


More information about the Libav-user mailing list