From a2e28989d501f97fa3894b9d5accbe5871c744ff Mon Sep 17 00:00:00 2001 From: cuixianming Date: Wed, 8 May 2019 19:28:54 +0800 Subject: [PATCH 1/3] Add version select by environment variable --- .gitignore | 1 + Cargo.toml | 5 +++-- README.md | 1 + build.rs | 43 ++++++++++++++++++++++++++----------------- 4 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 README.md diff --git a/.gitignore b/.gitignore index 90791199..c24dab46 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target Cargo.lock /tmp +/.idea \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 1703d2f5..90a7e561 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "ffmpeg-sys" -version = "4.0.2" +version = "4.1.3" build = "build.rs" links = "ffmpeg" -authors = ["meh. "] +authors = ["meh. ", "Xana Hopper "] license = "WTFPL" description = "FFI bindings to FFmpeg" @@ -20,6 +20,7 @@ cc = "1.0" pkg-config = "0.3" bindgen = "0.32" regex = "0.2" +version = "3.0.0" [features] default = ["avcodec", "avdevice", "avfilter", "avformat", "swresample", "swscale"] diff --git a/README.md b/README.md new file mode 100644 index 00000000..232f832f --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# FFmpeg bindingen for Rust diff --git a/build.rs b/build.rs index f570032e..60f7ec49 100644 --- a/build.rs +++ b/build.rs @@ -3,16 +3,18 @@ extern crate cc; extern crate num_cpus; extern crate pkg_config; extern crate regex; +extern crate version; use std::env; use std::fs::{self, create_dir, symlink_metadata, File}; use std::io::{self, BufRead, BufReader, Write}; use std::path::PathBuf; use std::process::Command; -use std::str; +use std::str::{self, FromStr}; use regex::Regex; use bindgen::callbacks::{IntKind, ParseCallbacks}; +use version::Version; #[derive(Debug)] struct Library { @@ -73,17 +75,24 @@ impl ParseCallbacks for IntCallbacks { } } -fn version() -> String { - let major: u8 = env::var("CARGO_PKG_VERSION_MAJOR") +fn branch_version() -> Version { + if let Ok(version) = env::var("FFMPEG_VERSION") { + Version::from_str(&version).unwrap_or(::version()) + } else { + version() + } +} + +fn version() -> Version { + let major: u32 = env::var("CARGO_PKG_VERSION_MAJOR") .unwrap() .parse() .unwrap(); - let minor: u8 = env::var("CARGO_PKG_VERSION_MINOR") + let minor: u32 = env::var("CARGO_PKG_VERSION_MINOR") .unwrap() .parse() .unwrap(); - - format!("{}.{}", major, minor) + Version { major, minor, patch: 0u32 } } fn output() -> PathBuf { @@ -91,7 +100,7 @@ fn output() -> PathBuf { } fn source() -> PathBuf { - output().join(format!("ffmpeg-{}", version())) + output().join(format!("ffmpeg-{}", branch_version())) } fn search() -> PathBuf { @@ -103,16 +112,16 @@ fn search() -> PathBuf { } fn fetch() -> io::Result<()> { - let status = try!( - Command::new("git") - .current_dir(&output()) - .arg("clone") - .arg("-b") - .arg(format!("release/{}", version())) - .arg("https://github.com/FFmpeg/FFmpeg") - .arg(format!("ffmpeg-{}", version())) - .status() - ); + let version = branch_version(); + println!("fetching FFmpeg {} to {}/ffmpeg-{}", version, output().to_str().unwrap(), version); + let status = Command::new("git") + .current_dir(&output()) + .arg("clone") + .arg("-b") + .arg(format!("n{}", version)) + .arg("https://github.com/FFmpeg/FFmpeg") + .arg(format!("ffmpeg-{}", version)) + .status()?; if status.success() { Ok(()) From a0385ee989c57e9a85337a441a6212f985b11bd4 Mon Sep 17 00:00:00 2001 From: cuixianming Date: Thu, 9 May 2019 16:44:05 +0800 Subject: [PATCH 2/3] change try to ? operator and update README --- README.md | 10 +++++++++- build.rs | 36 ++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 232f832f..5c61ece8 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# FFmpeg bindingen for Rust +# FFmpeg binding for Rust + +## Quick Start + +If setting environment variable `FFMPEG_VERSION` to X.X.X will fetch FFmpeg source from Github and build from it. +If setting environment variable `FFMPEG_DIR` to path that FFmpeg installed will skip build and link to it directly. + +Note that version must be full version included patch number. If none of above is set it will fetch FFmpeg which version matched ffmpeg-sys. + \ No newline at end of file diff --git a/build.rs b/build.rs index 60f7ec49..3aebdde3 100644 --- a/build.rs +++ b/build.rs @@ -175,13 +175,13 @@ fn build() -> io::Result<()> { ) } - // macro_rules! disable { - // ($conf:expr, $feat:expr, $name:expr) => ( - // if env::var(concat!("CARGO_FEATURE_", $feat)).is_err() { - // $conf.arg(concat!("--disable-", $name)); - // } - // ) - // } + macro_rules! disable { + ($conf:expr, $feat:expr, $name:expr) => ( + if env::var(concat!("CARGO_FEATURE_", $feat)).is_err() { + $conf.arg(concat!("--disable-", $name)); + } + ) + } // the binary using ffmpeg-sys must comply with GPL switch(&mut configure, "BUILD_LICENSE_GPL", "gpl"); @@ -276,24 +276,20 @@ fn build() -> io::Result<()> { } // run make - if !try!( - Command::new("make") - .arg("-j") - .arg(num_cpus::get().to_string()) - .current_dir(&source()) - .status() - ).success() + if !Command::new("make") + .arg("-j") + .arg(num_cpus::get().to_string()) + .current_dir(&source()) + .status()?.success() { return Err(io::Error::new(io::ErrorKind::Other, "make failed")); } // run make install - if !try!( - Command::new("make") - .current_dir(&source()) - .arg("install") - .status() - ).success() + if !Command::new("make") + .current_dir(&source()) + .arg("install") + .status()?.success() { return Err(io::Error::new(io::ErrorKind::Other, "make install failed")); } From 944646c89ba89a532236ea8a16f71dd07e6471af Mon Sep 17 00:00:00 2001 From: cuixianming Date: Thu, 9 May 2019 17:38:12 +0800 Subject: [PATCH 3/3] fix ffmpeg version issue when fetch --- Cargo.toml | 1 - build.rs | 36 +++++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90a7e561..ff9da3e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ cc = "1.0" pkg-config = "0.3" bindgen = "0.32" regex = "0.2" -version = "3.0.0" [features] default = ["avcodec", "avdevice", "avfilter", "avformat", "swresample", "swscale"] diff --git a/build.rs b/build.rs index 3aebdde3..81752949 100644 --- a/build.rs +++ b/build.rs @@ -3,18 +3,16 @@ extern crate cc; extern crate num_cpus; extern crate pkg_config; extern crate regex; -extern crate version; use std::env; use std::fs::{self, create_dir, symlink_metadata, File}; use std::io::{self, BufRead, BufReader, Write}; use std::path::PathBuf; use std::process::Command; -use std::str::{self, FromStr}; +use std::str; use regex::Regex; use bindgen::callbacks::{IntKind, ParseCallbacks}; -use version::Version; #[derive(Debug)] struct Library { @@ -75,15 +73,19 @@ impl ParseCallbacks for IntCallbacks { } } -fn branch_version() -> Version { +fn branch_version() -> String { if let Ok(version) = env::var("FFMPEG_VERSION") { - Version::from_str(&version).unwrap_or(::version()) + if version.is_empty() { + ::version() + } else { + version + } } else { version() } } -fn version() -> Version { +fn version() -> String { let major: u32 = env::var("CARGO_PKG_VERSION_MAJOR") .unwrap() .parse() @@ -92,7 +94,19 @@ fn version() -> Version { .unwrap() .parse() .unwrap(); - Version { major, minor, patch: 0u32 } + let patch: u32 = env::var("CARGO_PKG_VERSION_PATCH") + .unwrap() + .parse() + .unwrap(); + version_str(major, minor, patch) +} + +fn version_str(major: u32, minor: u32, patch: u32) -> String { + if patch == 0 { + format!("{}.{}", major, minor) + } else { + format!("{}.{}.{}", major, minor, patch) + } } fn output() -> PathBuf { @@ -175,14 +189,6 @@ fn build() -> io::Result<()> { ) } - macro_rules! disable { - ($conf:expr, $feat:expr, $name:expr) => ( - if env::var(concat!("CARGO_FEATURE_", $feat)).is_err() { - $conf.arg(concat!("--disable-", $name)); - } - ) - } - // the binary using ffmpeg-sys must comply with GPL switch(&mut configure, "BUILD_LICENSE_GPL", "gpl");