From df68733e933999d91b37b4830c3af2bea8fecfb3 Mon Sep 17 00:00:00 2001 From: Zen <46526140+master-of-zen@users.noreply.github.com> Date: Thu, 20 May 2021 09:31:17 +0300 Subject: [PATCH] ffmpeg module to rust. Ported get frame types --- av1an-core/src/ffmpeg.rs | 35 ++++++++++++++++++++++++++++++++ av1an-pyo3/src/lib.rs | 9 +++++++- av1an/ffmpeg/__init__.py | 1 - av1an/ffmpeg/ffmpeg.py | 44 ---------------------------------------- 4 files changed, 43 insertions(+), 46 deletions(-) delete mode 100644 av1an/ffmpeg/__init__.py delete mode 100755 av1an/ffmpeg/ffmpeg.py diff --git a/av1an-core/src/ffmpeg.rs b/av1an-core/src/ffmpeg.rs index f2a3334..004a576 100644 --- a/av1an-core/src/ffmpeg.rs +++ b/av1an-core/src/ffmpeg.rs @@ -220,3 +220,38 @@ pub fn concatenate_ffmpeg(temp: &Path, output: &Path, encoder: Encoder) { assert!(out.status.success()); } + +pub fn get_frame_types(file: &Path) -> Vec { + let mut cmd = Command::new("ffmpeg"); + + cmd.stdout(Stdio::piped()); + cmd.stderr(Stdio::piped()); + + let args = [ + "ffmpeg", + "-hide_banner", + "-i", + file.to_str().unwrap(), + "-vf", + "showinfo", + "-f", + "null", + "-loglevel", + "debug", + "-", + ]; + + cmd.args(args); + + let out = cmd.output().unwrap(); + + assert!(out.status.success()); + + let output = String::from_utf8(out.stderr).unwrap(); + + let str_vec = output.split("\n").collect::>(); + + let string_vec: Vec = str_vec.iter().map(|x| x.to_string()).collect(); + + string_vec +} diff --git a/av1an-pyo3/src/lib.rs b/av1an-pyo3/src/lib.rs index 829c0a5..10d84bb 100644 --- a/av1an-pyo3/src/lib.rs +++ b/av1an-pyo3/src/lib.rs @@ -107,6 +107,13 @@ fn get_ffmpeg_info() -> String { av1an_core::get_ffmpeg_info() } +#[pyfunction] +fn get_frame_types(file: String) -> Vec { + let input_file = Path::new(&file); + + av1an_core::ffmpeg::get_frame_types(input_file) +} + #[pyfunction] fn determine_workers(encoder: &str) -> PyResult { Ok(av1an_core::determine_workers( @@ -173,6 +180,6 @@ fn av1an_pyo3(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(construct_target_quality_command, m)?)?; m.add_function(wrap_pyfunction!(concatenate_ffmpeg, m)?)?; m.add_function(wrap_pyfunction!(extract_audio, m)?)?; - + m.add_function(wrap_pyfunction!(get_frame_types, m)?)?; Ok(()) } diff --git a/av1an/ffmpeg/__init__.py b/av1an/ffmpeg/__init__.py deleted file mode 100644 index 3667d3e..0000000 --- a/av1an/ffmpeg/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .ffmpeg import * \ No newline at end of file diff --git a/av1an/ffmpeg/ffmpeg.py b/av1an/ffmpeg/ffmpeg.py deleted file mode 100755 index 2833a33..0000000 --- a/av1an/ffmpeg/ffmpeg.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/env python - -import re -import subprocess -from pathlib import Path -from subprocess import PIPE, STDOUT -from typing import List -from av1an.logger import log - - -def get_frametypes(file: Path) -> List: - """ - Read file and return list with all frame types - :param file: Path for file - :return: list with sequence of frame types - """ - - frames = [] - - ff = [ - "ffmpeg", - "-hide_banner", - "-i", - file.as_posix(), - "-vf", - "showinfo", - "-f", - "null", - "-loglevel", - "debug", - "-", - ] - - pipe = subprocess.Popen(ff, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - - while True: - line = pipe.stdout.readline().strip().decode("utf-8") - - if len(line) == 0 and pipe.poll() is not None: - break - - frames.append(line) - - return frames