Disclaimer: This works in combination with a (free) third-party App Store app.
The code is here:
http://gist.github.com/4704121
Using the gist downloader or manually saving it into Pythonista, it should be named 'YoutubeDL' when looking at your list of scripts in the Pythonista interface.
In the comments, you'll notice a bookmarklet:
javascript:window.location='pythonista://YoutubeDL?action=run&argv='+encodeURIComponent(document.location.href);
In Mobile Safari, create a Bookmark for any page and name it something meaningful to you (mine is named: "--- m.Youtube DL"). Then go into Bookmark Edit mode and change the URL to the javascript listed above.
The app that this script works in combination with is called 'iDownloads PLUS - Download Manager!', free, from Amad Marwat, in the iOS app store.
How to use the script:
Browse to http://m.youtube.com in Mobile Safari, view a video that you like (press Stop if it starts playing, you need to be looking at the page - not the video actually playing), then click on "Bookmarks" and select the bookmarklet that you created.
This will launch my script, which will pull the URL of the page you were looking at as an argument, parse it, figure out the direct download URL for the .mp4 video file, then open iDownloads directly to that URL to start downloading it.
I picked iDownloads because it's free, can download in the background (up to 10 minutes, with notifications when complete), can download multiple files, can play the .mp4 files directly, has HTTP sharing and iTunes documents capabilities (for getting the video to a computer), can save .mp4 files to your Photo roll, and also has 'Open In...' capabilities to move them to another app.
When the download opens in iDownloads, it'll prompt with a few options - I usually choose 'Download As' and then give it a whatever.mp4 video file name.
There's some tweaks, if you look at the source code of the script, where you can change what your preferred default download type / quality is (flv, mp4, webm, 720p, 480p, etc.).
Why I wrote this:
Apple has continued to crack down on App Store authors that make video download utilities and disallow submission / updates for apps that provide the ability to download content from YouTube and save it to your device. Every app that ever offered this feature (free or paid for) has always eventually removed it claiming 'compliance with YouTube Terms of Service', but it's always been Apple twisting the wrist.
With this bit of code, in theory you could use any app in the App Store as long as it provided a custom URL opener (in this case 'iDownloads://') and took web URLs and offered to download them if they were not web pages. This script should be 'future proof' against Apple :)
I use it in combination with a few other tools that I have so that, without accessing a computer, in my house I can download a YouTube video and then save it to a Samba / SMB file share on the network - making it instantly available to my TVs (WiiMC and XBMC on an AppleTV). Makes it a breeze to download stuff for my 3 year old without having to find the laptop.