Closed Mach1212 closed 2 years ago
I also have the same problem. The vid I tried has 4k quality but the one that got downloaded is 1280x640 resolution.
The code I use is:
let best_quality = video.best_quality().unwrap();
let path_to_video = block!(best_quality.download_to_dir(Path::new("./videos"))).unwrap();
@Mach1212 @mehmetkursataydin thank you for mentioning this. The reason rustube
does not download the highest quality video in your cases is, that Video::best_quality
will only consider Stream
s with both an audio and a video track.
If you i.e. look at the Clash of Clans promo video streams, you'll notice, that all 1080p
streams are missing an audio track.
Unfortunately, YouTube does that quite often, that it splits the audio and video tracks into separate Stream
s. If you don't care about the audio track, you can call:
video
.streams()
.iter()
// you might also want to filter for `stream.mime.subtype() == mime::MP4` if you only want mp4 videos
.filter(|stream| stream.includes_video_track)
.max_by_key(|stream| stream.width)
This will get you the highest resolution video Stream
.
If you need both, the highest resolution video, and the best audio track, you'll have to download both tracks separately and combine them using something like ffmpeg
. That's probably also what video downloader websites do.
To get the highest quality audio track:
video
.streams()
.iter()
// you might also want to filter for a certain audio format, like mp4 or webm
.filter(|stream| stream.includes_audio_track)
.max_by_key(|stream| stream.bitrate)
@Mach1212 just as a quick note:
Be careful with .download_to(format!("{}_vid.mp4", id))
, since the video is not guaranteed to be in mp4
format. To get the correct extension, you can call stream.mime.subtype().as_str()
. If you only want mp4
videos, you can filter the streams as mentioned in the code comment above.
(the docs are outdated here, I will fix that)
Thanks for the in depth replies!
@Mach1212 @mehmetkursataydin thank you for mentioning this. The reason
rustube
does not download the highest quality video in your cases is, thatVideo::best_quality
will only considerStream
s with both an audio and a video track.If you i.e. look at the Clash of Clans promo video streams, you'll notice, that all
1080p
streams are missing an audio track.streams Unfortunately, YouTube does that quite often, that it splits the audio and video tracks into separate
Stream
s. If you don't care about the audio track, you can call:video .streams() .iter() // you might also want to filter for `stream.mime.subtype() == mime::MP4` if you only want mp4 videos .filter(|stream| stream.includes_video_track) .max_by_key(|stream| stream.width)
This will get you the highest resolution video
Stream
.If you need both, the highest resolution video, and the best audio track, you'll have to download both tracks separately and combine them using something like
ffmpeg
. That's probably also what video downloader websites do.
Thanks for the great explanation and sample codes!
Description
I am writing an app that needs to download google play store app promo videos. The videos are hosted on youtube and I am trying to download Clash of Clans promo video(https://www.youtube.com/embed/QHnxnzrwQmk?ps=play&vq=large&rel=0&autohide=1&showinfo=0), but Rustube keeps downloading it at 720p.
On Youtube I see an option for 1080p and when downloading via 4k Video Downloader, I also get 1080p.
My Code: let video = Video::from_url(&url).await.unwrap(); video .best_quality() .unwrap() .download_to(format!("{}_vid.mp4", id)) .await .unwrap();
Version