Skip to content

Commit

Permalink
opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
zmisgod committed Feb 24, 2024
1 parent 5a09d4a commit c45e443
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ffmpeg-tool-rs"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ffmpeg-tool-rs

一个关于ffmpeg的常用工具

## usage

### 多个视频合并成一个

比如当前有2个视频`IMG_1767.MOV`以及`IMG_1768.MOV`,需要将这2个视频合并成一个视频,
你需要指定`-r IMG_(.*).MOV`并且指定开始的id(`--reg-file-start=`)以及结束的id(`--reg-file-end=`)
则执行下面的命令即可,会得到一个`IMG_.MOV`的合并后的视频文件。

```
ffmpeg-tool-rs combine -r IMG_\(\.\*\).MOV --reg-file-start=1767 --reg-file-end=1768
```

当然也可以指定生成后的文件名,需要跟上`--target_file_name=your_filename.MOV`


### 下载视频

```
ffmpeg-tool-rs download --url=https://zmis.me/xxx.m3u8
```
9 changes: 1 addition & 8 deletions src/combine.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub mod parse {
use std::fmt::{Error, format};
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use tempfile::tempdir;
use std::fs::File;
use std::io::prelude::*;
use crate::common::now;

pub fn get_reg_files(reg_name: String, reg_start: i32, reg_end: i32) -> Result<Vec<String>, Error> {
let mut files = vec![];
Expand All @@ -19,13 +19,6 @@ pub mod parse {
return reg_name.replace("(.*)", "");
}

fn now() -> u64 {
let now = SystemTime::now();
return now.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs();
}

fn get_temp_file() -> String {
if let Ok(dir) = tempdir() {
if let Some(a) = dir.path().join(format!("{}.txt", now())).to_str() {
Expand Down
8 changes: 8 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::time::{SystemTime, UNIX_EPOCH};

pub fn now() -> u64 {
let now = SystemTime::now();
return now.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs();
}
30 changes: 30 additions & 0 deletions src/download.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pub mod download {
use std::fmt::{Error, format};
use std::process::Command;
use crate::common::now;

pub fn download(url: String, file_name: String) -> Result<bool, Error> {
let mut binding = Command::new("ffmpeg");
let res = binding.arg("-i")
.arg(url.to_owned())
.arg("-c")
.arg("copy")
.arg("-bsf:a")
.arg("aac_adtstoasc")
.arg(file_name.to_owned()).output().unwrap().status;
if res.success() {
Ok(true)
} else {
println!("{}", res.to_string());
Ok(false)
}
}

pub fn get_file_name(file_name: String) -> String {
let mut target = file_name;
if target.is_empty() {
target = format!("./{}.mp4", now());
}
target
}
}
61 changes: 42 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod combine;
mod download;
mod common;

use clap::{arg, Args as clapArgs, Parser, Subcommand};
use std::{env};
use crate::combine::parse::{to_files, get_reg_files, combine, get_reg_file_name, white_to_files};
use crate::download::download::{download, get_file_name};

#[derive(Parser)]
#[command(name = "ffmpeg-tool-rs")]
Expand All @@ -16,61 +19,65 @@ pub struct Args {
enum Commands {
/// 合并视频
Combine(CombineArgs),
// /// 下载视频
// Download(DownloadArgs),
/// 下载视频
Download(DownloadArgs),
}

#[derive(clapArgs)]
pub struct CombineArgs {
/* 完整的视频url,格式如下
-f https://zmis.me/video0.mp4 -f https://zmis.me/video1.mp4 -f https://zmis.me/video2.mp4
*/
#[arg(short = 'f', long = "files")]
files: Vec<String>,
// #[arg(short = 'f', long = "files")]
// files: Vec<String>,

/* 如果很多,可以放在文件中,格式如下
https://zmis.me/video0.mp4
https://zmis.me/video1.mp4
https://zmis.me/video2.mp4
*/
#[arg(short = 'l', long = "local-files", default_value_t = String::from(""))]
local: String,
// #[arg(short = 'l', long = "local-files", default_value_t = String::from(""))]
// local: String,

// 正则模式, 输入文件 https://zmis.me/video(.*).mp4
/// 正则模式, 输入文件 https://zmis.me/video(.*).mp4
#[arg(short = 'r', long = "reg-name", default_value_t = String::from(""))]
reg_name: String,

// 正则模式,输入的文件开始数字
/// 正则模式,输入的文件开始数字
#[arg(long = "reg-file-start")]
reg_name_start: i32,

// 正则模式,输入的文件结束数字
/// 正则模式,输入的文件结束数字
#[arg(long = "reg-file-end")]
reg_name_end: i32,

// 输出的文件目录
/// 输出的文件目录
#[arg(long = "target_folder", default_value_t = String::from(""))]
target_folder: String,

// 输出的文件名
/// 输出的文件名
#[arg(long = "target_file_name", default_value_t = String::from(""))]
target_file_name: String,
}

#[derive(clapArgs)]
pub struct DownloadArgs {}
pub struct DownloadArgs {
/// m3u8链接地址
#[arg(short = 'u', long = "url")]
url: String,

/// 输出的文件名
#[arg(long = "target_file_name", default_value_t = String::from(""))]
target_file_name: String,
}

#[actix_web::main]
pub async fn main() {
let args = Args::parse();
match args.command {
Commands::Combine(args) => {
for value in args.files {
println!("{}", value);
}
let files = get_reg_files(args.reg_name.clone(), args.reg_name_start, args.reg_name_end).expect("解析失败");
let file_name = to_files().expect("生成文件失败");
println!("file name = {}", file_name.to_owned());
let mut target = String::default();
if args.target_file_name.is_empty() {
target = format!("./{}", get_reg_file_name(args.reg_name.to_owned()));
Expand All @@ -82,10 +89,26 @@ pub async fn main() {
}
}
white_to_files(files.clone(), file_name.clone()).expect("写入文件失败");

println!("{}", target);
let res = combine(file_name.clone(), target).expect("合并文件失败");
println!("res {}", res);
if res {
println!("合并文件成功")
} else {
println!("合并文件失败")
}
}
Commands::Download(args) => {
if args.url.is_empty() {
println!("url is required!");
return;
}
let file_name = get_file_name(args.target_file_name.to_owned());
println!("download file name: {}", file_name.clone());
let res = download(args.url, file_name).expect("下载失败");
if res {
println!("下载成功")
} else {
println!("下载失败")
}
}
}
}

0 comments on commit c45e443

Please sign in to comment.