Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. extern crate walkdir;
  2. use std::fs;
  3. use std::env;
  4. use std::io;
  5. use std::string::ToString;
  6. use std::process::Command;
  7. use std::path::Path;
  8. use walkdir::WalkDir;
  9.  
  10. fn parse_dir(dir: &Path) {
  11. let paths = WalkDir::new(dir);
  12. for path in paths {
  13. let file = path.unwrap().path().display().to_string(); //convert std::path to string
  14. if file.contains(".mp4") {
  15. let new_file = file.clone() + ".encoded";
  16. encode(&file, &new_file);
  17. }
  18. }
  19. }
  20.  
  21. fn encode(file: &str, new_file: &str) {
  22. let encoding = Command::new("ffmpeg").arg("-i").arg(file).arg("-f").arg("mp4").arg(new_file).output().unwrap_or_else(|e| {
  23. panic!("failed to execute process: {}", e);
  24. });
  25. println!("{}", String::from_utf8_lossy(&encoding.stdout));
  26. let new_name = file.clone().to_string() + ".old";
  27. rename_files(file, &new_name);
  28. rename_files(new_file, file);
  29. }
  30.  
  31. fn rename_files(file: &str, new_name: &str) -> io::Result<()> {
  32. try!(fs::rename(file, new_name));
  33. Ok(())
  34. }
  35.  
  36. fn check_dir(dir: &Path) -> io::Result<()> {
  37. if try!(fs::metadata(dir)).is_dir() {
  38. parse_dir(dir);
  39. }
  40. Ok(())
  41. }
  42.  
  43. fn main() {
  44. let args: Vec<String> = env::args().collect();
  45. if args.len() > 1 {
  46. let dir = Path::new(&args[1]);
  47. check_dir(dir);
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement