diff --git a/libavformat/avio.c b/libavformat/avio.c
index a2b8a8325903bcf969cb17dcbcfa4e60ff289884..f2fe0e13c681b114fc23b496a98c861617d4946c 100644
--- a/libavformat/avio.c
+++ b/libavformat/avio.c
@@ -148,9 +148,12 @@ offset_t url_filesize(URLContext *h)
 {
     offset_t pos, size;
 
+    size= url_seek(h, 0, AVSEEK_SIZE);
+    if(size<0){
     pos = url_seek(h, 0, SEEK_CUR);
     size = url_seek(h, -1, SEEK_END)+1;
     url_seek(h, pos, SEEK_SET);
+    }
     return size;
 }
 
diff --git a/libavformat/avio.h b/libavformat/avio.h
index f0fd1a85cf4802a11cc26c8191b4662f22a82cb3..27fb241e337c5d09eb1ff8d98fbc321b4243d8fd 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -69,6 +69,8 @@ void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
 /* not implemented */
 int url_poll(URLPollEntry *poll_table, int n, int timeout);
 
+#define AVSEEK_SIZE 0x10000
+
 typedef struct URLProtocol {
     const char *name;
     int (*url_open)(URLContext *h, const char *filename, int flags);
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index f3e7d5081fa1f3a8e97720f0eb30aa061bc14eee..450b4cf11d3bace688859b11e928d832968e958f 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -170,8 +170,11 @@ offset_t url_fsize(ByteIOContext *s)
 
     if (!s->seek)
         return -EPIPE;
+    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
+    if(size<0){
     size = s->seek(s->opaque, -1, SEEK_END) + 1;
     s->seek(s->opaque, s->pos, SEEK_SET);
+    }
     return size;
 }