0

How can I get a youtube video duration from it’s URL with bash? Preferably using curl, and without having to mess with authentications and API keys.

If possible, I’d like to know the details (i.e. I think the results are given in json, so maybe a way to get only the duration from that), but getting the duration is the main problem.

1 Answer 1

2

Well, if you don't want to use the API and would like to use curl, here's a super ugly answer to match :)

Marks-MacBook-Air:~ mstanislav$ curl -s http://www.youtube.com/watch?v=dQw4w9WgXcQ |grep '<meta itemprop="duration"'
<meta itemprop="duration" content="PT3M33S">

The PT3M33S means 3 minutes and 33 seconds, which is the duration. Just process the data from there (via shell script or otherwise you're using curl with). Be kind voters, just answering the question as requested ;)

For the sake of everything happy, here's using curl with the API, which, does not require a developer key unless you are uploading or writing data.

curl -s 'http://gdata.youtube.com/feeds/api/videos/dQw4w9WgXcQ?v=2&alt=jsonc'
Sign up to request clarification or add additional context in comments.

3 Comments

I don’t exactly have anything against the API, but from what I saw it seems that I’d need to register the app to get a key, and that’s too much hassle for such a simple task (if I’m wrong in this regard, you can share the answer with the API if you prefer). It’s not that ugly, it works as intended. It’s not blazing fast, but it’ll do very well. Thank you.
Updated with sane way, using API. As I state there, no API key needed for just a simple video query. Look for 'duration' which is in seconds. Enjoy!
Ironically enough, the “ugly” way is safer. Since youtube urls don’t always follow the same scheme (eg, lists), getting the video ID can be tricky, which makes the first way more fool‐proof.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.