Spaces:
Sleeping
Sleeping
| 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) | |