Spaces:
Sleeping
Sleeping
File size: 3,445 Bytes
29395b7 616c7a5 29395b7 616c7a5 29395b7 616c7a5 29395b7 616c7a5 29395b7 616c7a5 29395b7 616c7a5 29395b7 dfb4678 0aaee64 dfb4678 0104874 dfb4678 616c7a5 29395b7 616c7a5 29395b7 616c7a5 dfb4678 29395b7 3172ebc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import gradio as gr
from pydub import AudioSegment
import tempfile
import os
def process_audio(file,
left_source, # "left", "right", "mute"
right_source, # "left", "right", "mute"
output_format # "元のまま", "wav", "mp3", ...
):
# 入力フォーマットを取得
original_format = os.path.splitext(file)[1][1:] # 例: ".wav" → "wav"
# ステレオで読み込み
audio = AudioSegment.from_file(file)
left, right = audio.split_to_mono()
# ソース選択
def get_channel(source):
if source == "left":
return left
elif source == "right":
return right
else: # mute
return AudioSegment.silent(duration=len(audio))
new_left = get_channel(left_source)
new_right = get_channel(right_source)
# ステレオ再構築
combined = AudioSegment.from_mono_audiosegments(new_left, new_right)
# 出力フォーマットを決定
export_format = original_format if output_format == "元のまま" else output_format
suffix = f".{export_format}"
# 一時ファイルに保存
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmpfile:
combined.export(tmpfile.name, format=export_format)
return tmpfile.name
# UI設定
options = ["left", "right", "mute"]
format_options = ["元のまま", "wav", "mp3", "ogg", "flac"]
theme = gr.themes.Base(
primary_hue="sky",
neutral_hue="slate",
text_size="lg",
radius_size="sm",
font=[gr.themes.GoogleFont('Zen Maru Gothic'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
font_mono=[gr.themes.GoogleFont('Kosugi Maru'), 'ui-monospace', 'Consolas', 'monospace'],
).set(
body_text_weight='500',
embed_radius='*radius_xxs',
border_color_primary='*neutral_400',
border_color_primary_dark='*neutral_400',
shadow_drop='*block_shadow',
shadow_drop_lg='*shadow_drop',
button_border_width='*block_border_width',
button_border_width_dark='*block_border_width',
button_primary_shadow='*shadow_drop',
button_primary_shadow_hover='*shadow_inset',
button_primary_shadow_active='*shadow_drop',
button_secondary_shadow='*shadow_drop',
button_secondary_shadow_hover='*shadow_inset',
button_secondary_shadow_active='*shadow_drop',
button_large_padding='*spacing_md',
button_large_radius='*radius_sm',
button_small_radius='*radius_xxl',
button_primary_background_fill='*primary_100',
button_primary_background_fill_dark='*primary_500',
button_primary_background_fill_hover_dark='*button_secondary_background_fill_hover',
)
demo = gr.Interface(
fn=process_audio,
inputs=[
gr.Audio(type="filepath", label="音声ファイル(ステレオ)"),
gr.Radio(choices=options, value="left", label="左チャンネルに流す音"),
gr.Radio(choices=options, value="right", label="右チャンネルに流す音"),
gr.Dropdown(choices=format_options, value="元のまま", label="出力フォーマット")
],
outputs=gr.Audio(label="出力音声(設定済みステレオ)"),
title="ステレオチャンネル編集ツール",
description="左・右チャンネルの音声ソースを個別に設定し、出力フォーマットを選んで保存できます。",
theme=theme
)
if __name__ == "__main__":
demo.launch(debug=True)
|