Skip to content

Commit

Permalink
Some ffmpeg utils
Browse files Browse the repository at this point in the history
  • Loading branch information
gitmylo committed Jun 30, 2023
1 parent 7d81d24 commit 91c7d61
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
55 changes: 55 additions & 0 deletions webui/modules/implementations/ffmpeg_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import shlex
import subprocess
from tempfile import NamedTemporaryFile

import gradio

from setup_tools.os import is_windows


def run_command(command) -> subprocess.CompletedProcess:
if not is_windows():
command = shlex.split(command)
return subprocess.run(command)


def image_audio():
with gradio.Row():
with gradio.Column():
image = gradio.Image(label='Image', type='filepath')
audio = gradio.Audio(label='Audio', type='filepath')
output = gradio.PlayableVideo(label='Output video')
combine_button = gradio.Button('Combine', variant='primary')

def image_audio_func(i, a):
out_file = NamedTemporaryFile(delete=False, suffix='.mp4').name
result = run_command(f'ffmpeg -y -loop 1 -i "{i}" -i "{a}" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest "{out_file}"')
assert result.returncode == 0
return out_file

combine_button.click(fn=image_audio_func, inputs=[image, audio], outputs=output)


def video_audio():
with gradio.Row():
with gradio.Column():
video = gradio.Video(label='Video')
audio = gradio.Audio(label='Audio', type='filepath')
output = gradio.PlayableVideo(label='Output video')
combine_button = gradio.Button('Combine', variant='primary')

def image_audio_func(v, a):
out_file = NamedTemporaryFile(delete=False, suffix='.mp4').name
result = run_command(f'ffmpeg -y -i "{v}" -i "{a}" -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -b:a 192k -shortest "{out_file}"')
assert result.returncode == 0
return out_file

combine_button.click(fn=image_audio_func, inputs=[video, audio], outputs=output)


def ffmpeg_utils_tab():
with gradio.Tabs():
with gradio.Tab('🖼 + 🔊 = 📽'):
image_audio()
with gradio.Tab('📽 + 🔊 = 📽'):
video_audio()
2 changes: 0 additions & 2 deletions webui/ui/common.py

This file was deleted.

7 changes: 5 additions & 2 deletions webui/ui/tabs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import torchaudio.functional as F

import webui.ui.tabs.rvc as rvc
from webui.modules.implementations.ffmpeg_utils import ffmpeg_utils_tab


def denoise_tab():
Expand Down Expand Up @@ -195,7 +196,9 @@ def utils_tab():
denoise_tab()
with gradio.Tab('🔊▶🗣/🎵 music splitting'):
music_split_tab()
with gradio.Tab('🔽 audio downloads'):
audio_download_tab()
with gradio.Tab('📈 audio waveforms'):
waveform_tab()
with gradio.Tab('👽 FFMPEG'):
ffmpeg_utils_tab()
with gradio.Tab('🔽 audio downloads'):
audio_download_tab()

0 comments on commit 91c7d61

Please sign in to comment.