背景
最近晚上给娃听故事
这页面不能连续播放,想着下载下来用默认播放器播放好了, 刚好最近在学rust,就用rust实现mp4下载.
动手
- 整理下载链接
由于链接可能涉及不必要的纠纷就先不明文列出来了
- 编码
- 解析json
rust
use serde::{Deserialize,Serialize};
use serde_json;
#[derive(Debug,Deserialize,Serialize)]
struct Audio {
title:String,
src:String,
}
fn main() {
println!("download mp4!");
let json_urls = r#"[
{"title":"第一辑 吃饭","src":"http://tcv.clewm.net"},
{"title":"第一辑.大声回应","src":"http://tcv.clewm.net"},
{"title":"第一辑.好朋友","src":"http://tcv.clewm.net"},
{"title":"第一辑.拉粑粑","src":"http://tcv.clewm.net"},
{"title":"第一辑.你好","src":"http://tcv.clewm.net"},
{"title":"第一辑.尿床了","src":"http://tcv.clewm.net"},
{"title":"第一辑.刷牙","src":"http://tcv.clewm.net"},
{"title":"第一辑.谁哭了","src":"http://tcv.clewm.net"},
{"title":"第一辑.睡觉","src":"http://tcv.clewm.net"},
{"title":"第一辑.洗澡","src":"http://tcv.clewm.net"},
{"title":"第二辑.对不起","src":"http://tcv.clewm.net"},
{"title":"第二辑.多吃蔬菜","src":"http://tcv.clewm.net"},
{"title":"第二辑.多吃水果","src":"http://tcv.clewm.net"},
{"title":"第二辑.过生日","src":"http://tcv.clewm.net"},
{"title":"第二辑.排好队","src":"http://tcv.clewm.net"},
{"title":"第二辑.散步","src":"http://tcv.clewm.net"},
{"title":"第二辑.收起来","src":"http://tcv.clewm.net"},
{"title":"第二辑.我会穿鞋子","src":"http://tcv.clewm.net"},
{"title":"第二辑.我会穿衣服","src":"http://tcv.clewm.net"},
{"title":"第二辑.有危险","src":"http://tcv.clewm.net"},
{"title":"第三辑.不赖床","src":"http://tcv.clewm.net"},
{"title":"第三辑.插座上的洞洞","src":"http://tcv.clewm.net"},
{"title":"第三辑.地上的东西是糖吗","src":"http://tcv.clewm.net"},
{"title":"第三辑.好烫的水","src":"http://tcv.clewm.net"},
{"title":"第三辑.客人来我家","src":"http://tcv.clewm.net"},
{"title":"第三辑.妈妈不见了","src":"http://tcv.clewm.net"},
{"title":"第三辑.勤洗手","src":"http://tcv.clewm.net"},
{"title":"第三辑.讨厌的桌角","src":"http://tcv.clewm.net"},
{"title":"第三辑.谢谢","src":"http://tcv.clewm.net"},
{"title":"第三辑.站在椅子上","src":"http://tcv.clewm.net"}
]"#;
let audios = serde_json::from_str::<Vec<Audio>>(json_urls).unwrap();
- 下载文件
rust
use std::fs::File;
use std::path::Path;
use std::io::prelude::*;
use reqwest;
async fn download(audio:Audio)->std::result::Result<(),Box<dyn std::error::Error>>{
println!("downloading {}",audio.title);
let client = reqwest::Client::new();
let body = client.get(&audio.src).send().await.unwrap().bytes().await.unwrap();
let title = String::from("./")+&audio.title+".mp4";
let path = Path::new(&title);
let mut file = File::create(&path).unwrap();
let content = body.bytes();
let data:std::result::Result<Vec<_>,_> = content.collect();
file.write_all(&data.unwrap()).unwrap();
println!("downloaded {}",audio.title);
Ok(())
}
- 完整代码
rust
use serde::{Deserialize,Serialize};
use serde_json;
use std::fs::File;
use std::path::Path;
use std::io::prelude::*;
use reqwest;
#[derive(Debug,Deserialize,Serialize)]
struct Audio {
title:String,
src:String,
}
async fn download(audio:Audio)->std::result::Result<(),Box<dyn std::error::Error>>{
println!("downloading {}",audio.title);
let client = reqwest::Client::new();
let body = client.get(&audio.src).send().await.unwrap().bytes().await.unwrap();
let title = String::from("./")+&audio.title+".mp4";
let path = Path::new(&title);
let mut file = File::create(&path).unwrap();
let content = body.bytes();
let data:std::result::Result<Vec<_>,_> = content.collect();
file.write_all(&data.unwrap()).unwrap();
println!("downloaded {}",audio.title);
Ok(())
}
#[tokio::main]
async fn main() {
println!("download mp4!");
let json_urls = r#"[
{"title":"第一辑 吃饭","src":"http://tcv.clewm.net"},
{"title":"第一辑.大声回应","src":"http://tcv.clewm.net"},
{"title":"第一辑.好朋友","src":"http://tcv.clewm.net"},
{"title":"第一辑.拉粑粑","src":"http://tcv.clewm.net"},
{"title":"第一辑.你好","src":"http://tcv.clewm.net"},
{"title":"第一辑.尿床了","src":"http://tcv.clewm.net"},
{"title":"第一辑.刷牙","src":"http://tcv.clewm.net"},
{"title":"第一辑.谁哭了","src":"http://tcv.clewm.net"},
{"title":"第一辑.睡觉","src":"http://tcv.clewm.net"},
{"title":"第一辑.洗澡","src":"http://tcv.clewm.net"},
{"title":"第二辑.对不起","src":"http://tcv.clewm.net"},
{"title":"第二辑.多吃蔬菜","src":"http://tcv.clewm.net"},
{"title":"第二辑.多吃水果","src":"http://tcv.clewm.net"},
{"title":"第二辑.过生日","src":"http://tcv.clewm.net"},
{"title":"第二辑.排好队","src":"http://tcv.clewm.net"},
{"title":"第二辑.散步","src":"http://tcv.clewm.net"},
{"title":"第二辑.收起来","src":"http://tcv.clewm.net"},
{"title":"第二辑.我会穿鞋子","src":"http://tcv.clewm.net"},
{"title":"第二辑.我会穿衣服","src":"http://tcv.clewm.net"},
{"title":"第二辑.有危险","src":"http://tcv.clewm.net"},
{"title":"第三辑.不赖床","src":"http://tcv.clewm.net"},
{"title":"第三辑.插座上的洞洞","src":"http://tcv.clewm.net"},
{"title":"第三辑.地上的东西是糖吗","src":"http://tcv.clewm.net"},
{"title":"第三辑.好烫的水","src":"http://tcv.clewm.net"},
{"title":"第三辑.客人来我家","src":"http://tcv.clewm.net"},
{"title":"第三辑.妈妈不见了","src":"http://tcv.clewm.net"},
{"title":"第三辑.勤洗手","src":"http://tcv.clewm.net"},
{"title":"第三辑.讨厌的桌角","src":"http://tcv.clewm.net"},
{"title":"第三辑.谢谢","src":"http://tcv.clewm.net"},
{"title":"第三辑.站在椅子上","src":"http://tcv.clewm.net"}
]"#;
let audios = serde_json::from_str::<Vec<Audio>>(json_urls).unwrap();
for audio in audios {
let _ = download(audio).await;
}
}
- 结果