mirror of
https://github.com/facefusion/facefusion.git
synced 2026-07-14 05:57:23 +02:00
Feat/audio metadata (#1019)
* audio metadata * audio metadata * audio metadata * audio metadata * audio metadata * audio metadata * audio metadata
This commit is contained in:
@@ -4,6 +4,7 @@ from starlette.datastructures import Headers
|
||||
from starlette.types import Scope
|
||||
|
||||
from facefusion.audio import detect_audio_duration
|
||||
from facefusion.ffprobe import detect_audio_channel_total, detect_audio_format, detect_audio_frame_total, detect_audio_sample_rate
|
||||
from facefusion.types import AudioMetadata, ImageMetadata, VideoMetadata
|
||||
from facefusion.vision import count_video_frame_total, detect_image_resolution, detect_video_duration, detect_video_fps, detect_video_resolution
|
||||
|
||||
@@ -21,7 +22,11 @@ def get_sec_websocket_protocol(scope : Scope) -> Optional[str]:
|
||||
def extract_audio_metadata(file_path : str) -> AudioMetadata:
|
||||
metadata : AudioMetadata =\
|
||||
{
|
||||
'duration' : detect_audio_duration(file_path)
|
||||
'duration' : detect_audio_duration(file_path),
|
||||
'sample_rate' : detect_audio_sample_rate(file_path),
|
||||
'frame_total' : detect_audio_frame_total(file_path),
|
||||
'channels' : detect_audio_channel_total(file_path),
|
||||
'format' : detect_audio_format(file_path)
|
||||
}
|
||||
return metadata
|
||||
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import os
|
||||
import subprocess
|
||||
from typing import List, Optional
|
||||
|
||||
from facefusion import ffprobe_builder
|
||||
from facefusion.types import Command
|
||||
|
||||
|
||||
def run_ffprobe(commands : List[Command]) -> subprocess.Popen[bytes]:
|
||||
commands = ffprobe_builder.run(commands)
|
||||
return subprocess.Popen(commands, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
|
||||
|
||||
|
||||
def detect_audio_sample_rate(audio_path : str) -> Optional[int]:
|
||||
commands = ffprobe_builder.chain(
|
||||
ffprobe_builder.select_audio_stream(0),
|
||||
ffprobe_builder.show_stream_entries([ 'sample_rate' ]),
|
||||
ffprobe_builder.format_to_value(),
|
||||
ffprobe_builder.set_input(audio_path)
|
||||
)
|
||||
process = run_ffprobe(commands)
|
||||
output, _ = process.communicate()
|
||||
|
||||
if process.returncode == 0 and output:
|
||||
return int(output.decode().strip())
|
||||
return None
|
||||
|
||||
|
||||
def detect_audio_channel_total(audio_path : str) -> Optional[int]:
|
||||
commands = ffprobe_builder.chain(
|
||||
ffprobe_builder.select_audio_stream(0),
|
||||
ffprobe_builder.show_stream_entries([ 'channels' ]),
|
||||
ffprobe_builder.format_to_value(),
|
||||
ffprobe_builder.set_input(audio_path)
|
||||
)
|
||||
process = run_ffprobe(commands)
|
||||
output, _ = process.communicate()
|
||||
|
||||
if process.returncode == 0 and output:
|
||||
return int(output.decode().strip())
|
||||
return None
|
||||
|
||||
|
||||
def detect_audio_frame_total(audio_path : str) -> Optional[int]:
|
||||
commands = ffprobe_builder.chain(
|
||||
ffprobe_builder.select_audio_stream(0),
|
||||
ffprobe_builder.show_stream_entries([ 'duration', 'sample_rate' ]),
|
||||
ffprobe_builder.format_to_key_value(),
|
||||
ffprobe_builder.set_input(audio_path)
|
||||
)
|
||||
process = run_ffprobe(commands)
|
||||
output, _ = process.communicate()
|
||||
|
||||
if process.returncode == 0 and output:
|
||||
duration = None
|
||||
sample_rate = None
|
||||
lines = output.decode().strip().split(os.linesep)
|
||||
|
||||
for line in lines:
|
||||
if line.startswith('duration='):
|
||||
duration = float(line.split('=')[1])
|
||||
if line.startswith('sample_rate='):
|
||||
sample_rate = int(line.split('=')[1])
|
||||
|
||||
if duration and sample_rate:
|
||||
return int(duration * sample_rate)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def detect_audio_format(audio_path : str) -> Optional[str]:
|
||||
commands = ffprobe_builder.chain(
|
||||
ffprobe_builder.select_audio_stream(0),
|
||||
ffprobe_builder.show_stream_entries([ 'codec_name' ]),
|
||||
ffprobe_builder.format_to_value(),
|
||||
ffprobe_builder.set_input(audio_path)
|
||||
)
|
||||
process = run_ffprobe(commands)
|
||||
output, _ = process.communicate()
|
||||
|
||||
if process.returncode == 0 and output:
|
||||
return output.decode().strip()
|
||||
return None
|
||||
@@ -0,0 +1,33 @@
|
||||
import itertools
|
||||
import shutil
|
||||
from typing import List
|
||||
|
||||
from facefusion.types import Command
|
||||
|
||||
|
||||
def run(commands : List[Command]) -> List[Command]:
|
||||
return [ shutil.which('ffprobe'), '-loglevel', 'error' ] + commands
|
||||
|
||||
|
||||
def chain(*commands : List[Command]) -> List[Command]:
|
||||
return list(itertools.chain(*commands))
|
||||
|
||||
|
||||
def select_audio_stream(index : int) -> List[Command]:
|
||||
return [ '-select_streams', 'a:' + str(index) ]
|
||||
|
||||
|
||||
def show_stream_entries(entries : List[str]) -> List[Command]:
|
||||
return [ '-show_entries', 'stream=' + ','.join(entries) ]
|
||||
|
||||
|
||||
def format_to_value() -> List[Command]:
|
||||
return [ '-of', 'default=noprint_wrappers=1:nokey=1' ]
|
||||
|
||||
|
||||
def format_to_key_value() -> List[Command]:
|
||||
return [ '-of', 'default=noprint_wrappers=1' ]
|
||||
|
||||
|
||||
def set_input(input_path : str) -> List[Command]:
|
||||
return [ input_path ]
|
||||
+5
-1
@@ -174,7 +174,11 @@ AssetType = Literal['source', 'target']
|
||||
MediaType = Literal['image', 'video', 'audio']
|
||||
AudioMetadata = TypedDict('AudioMetadata',
|
||||
{
|
||||
'duration' : Duration
|
||||
'duration' : Duration,
|
||||
'sample_rate': int,
|
||||
'frame_total': int,
|
||||
'channels': int,
|
||||
'format': str
|
||||
})
|
||||
ImageMetadata = TypedDict('ImageMetadata',
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user