I read somewhere that youtube splits the video in many short files and they are stitched together just while playing. This makes downloading the video difficult.
I am wondering, there must be a way to hack into it and download all types of video, even live streaming video.
Could anyone point me to some doc or code etc, which explains how I can do it. I want to do it on ubuntu machine.
I am curious how does some site is able to do it?
An alternative option could be to play the video on full screen and record it using some screen recorder. But this will require a dedicated machine for doing this. Any suggestions?
The 'youtube-dl' program (which you mentioned in a tag in your question) does all this for you - it finds, downloads and stitches all the files together and outputs a single video file. You should get the latest version and not use the one provided by your distro (the distro package is likely out of date).
It is 'difficult' because Google doesn't want it to be easy - the publisher of a video uploads it to youtube for it to be watched, not downloaded and possibly broadcast elsewhere, so measures are taken to enforce this. Besides the publisher's rights, Google also stands to lose if the file becomes available for viewing without ads.
(BTW: it used to be that content authors could actually allow download explicitly, adding a download link to the video's page - I don't know if that's still the case).
In theory, there is always a way to 'hack' it, in the sense that whatever your browser can do can also be done without displaying the video, but saving it instead (e.g., you could get the entire browser source and modify it to do this - the result will contain all the ads, etc., exactly as displayed). While doable, it is difficult enough - and not a worthy goal.
Youtube-dl knows how the site's own javascript works and imitates it - but it doesn't actually run any actual youtube code, so it has to be updated as youtube gets updated. Therefore, it will always be somewhat out-of-date and some videos won't download.
Do not abuse Youtube, Google isn't going to take kindly to this. It is best not try to download more videos than you can actually watch in a given period of time - youtube will detect this and require you to solve a Captcha puzzle to prove you're not a robot trying to mass-download content (and eventually might block you altogether).