# Finding the size of a file on web server?

**URL:** <https://www.ruby-forum.com/t/finding-the-size-of-a-file-on-web-server/98582>\
**Category:** Ruby\
**Created:** [July 16, 2006, 10:25am UTC](https://www.ruby-forum.com/t/finding-the-size-of-a-file-on-web-server/98582 "2006-07-16T10:25:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [July 16, 2006, 10:25am UTC](https://www.ruby-forum.com/t/finding-the-size-of-a-file-on-web-server/98582/1 "2006-07-16T10:25:59Z")

</div>

I’m writing a little script to help automate downloading videos off of  
YouTube, and I would like to know how to figure out the size of the  
video files I will be downloading. I’ve experimented briefly with  
Net::HTTP, but it’s a little beyond me, as I haven’t been able to  
figure it out.

The URLs I’m using look like such:

[http://www.youtube.com/get\_video?video\_id=put\_real\_video\_id\_here](http://www.youtube.com/get_video?video_id=put_real_video_id_here)

and I’m getting the video id by a simple regexp. The trouble isn’t  
finding the file size, per-se, but finding it _before_ I download the  
entire thing.

I don’t know if this is possible or not, but any help on the subject  
would be greatly appreciated!

Thank You

---

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [July 16, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/finding-the-size-of-a-file-on-web-server/98582/2 "2006-07-16T13:26:22Z")

</div>

“CBlair1986” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> I’m writing a little script to help automate downloading videos off of  
> YouTube, and I would like to know how to figure out the size of the  
> video files I will be downloading. I’ve experimented briefly with  
> Net::HTTP, but it’s a little beyond me, as I haven’t been able to  
> figure it out.

Well, this depends on what you want to do - do you want to ask for the  
size of each file separately, or is it acceptable if you ask to get  
the download, and can get information about the size of the file the  
instant that the download starts?

In the first case, it depends on what information youtube is willing  
to give out in response to a HEAD request - not every webserver will  
give you a content-length header in HEAD, or even necessarily allow  
HEAD requests.

To test the first case, try this in irb:  
require ‘net/http’  
myvidlen = Net::HTTP.start(‘[www.youtube.com](http://www.youtube.com)’, 80) {|http|  
http.head(‘/get\_video?video\_id=real\_video\_id’).content\_length  
}

For the second case, you’re going to need to do something like this:  
require ‘net/http’  
Net::HTTP.start(‘[www.youtube.com](http://www.youtube.com)’, 80) {|http|  
http.request\_get(‘/get\_video?video\_id=real\_video\_id’) {|resp|  
myvidlen = resp.content\_length  
puts “Video real\_video\_id has length #{myvidlen}”  
# use the length to do something, like choose a directory  
File.open(‘whereISaveMyVideo.whatever’, ‘wb’) {|f|  
response.read\_body { |data|  
f.print(data)  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [July 16, 2006, 8:37pm UTC](https://www.ruby-forum.com/t/finding-the-size-of-a-file-on-web-server/98582/3 "2006-07-16T20:37:05Z")

</div>

Well, the first method doesn’t work, and the second method isn’t  
something that I’m looking for. I’d like to confirm if I’d actually  
like to download the file. Thanks anyways.
