first commit

This commit is contained in:
hina ntki 2024-08-03 20:29:08 +09:00
commit 44f598f7b5
7 changed files with 288 additions and 0 deletions

178
.gitignore vendored Normal file
View File

@ -0,0 +1,178 @@
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
### Python Patch ###
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
poetry.toml
# ruff
.ruff_cache/
# LSP config files
pyrightconfig.json
# End of https://www.toptal.com/developers/gitignore/api/python
web/static/*

7
docker-compose.yml Normal file
View File

@ -0,0 +1,7 @@
services:
web:
build: ./web
volumes:
- ./web/static/uploads:/app/static/uploads
ports:
- "5001:5000"

16
web/Dockerfile Normal file
View File

@ -0,0 +1,16 @@
# ベースイメージ
FROM python:3.9-slim
# 作業ディレクトリの設定
WORKDIR /app
# 必要なパッケージのインストール
RUN apt-get update && apt-get install -y ffmpeg
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# アプリケーションファイルのコピー
COPY . .
# コンテナ起動時に実行されるコマンド
CMD ["python", "app.py"]

45
web/app.py Normal file
View File

@ -0,0 +1,45 @@
from flask import Flask, request, send_file, render_template
import os
from process_audio import process_mp3
from zipfile import ZipFile
import io
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'static/uploads'
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'files' not in request.files:
return "No file part"
files = request.files.getlist('files')
if not files:
return "No selected file"
processed_files = []
for file in files:
if file and file.filename.endswith('.mp3'):
filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(filepath)
processed_filepath = process_mp3(filepath)
processed_files.append(processed_filepath)
else:
return "Invalid file type"
# ZIPファイルの作成
zip_buffer = io.BytesIO()
with ZipFile(zip_buffer, 'w') as zip_file:
for processed_file in processed_files:
zip_file.write(processed_file, os.path.basename(processed_file))
zip_buffer.seek(0)
return send_file(zip_buffer, as_attachment=True, download_name='intro_files.zip', mimetype='application/zip')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

26
web/process_audio.py Normal file
View File

@ -0,0 +1,26 @@
from pydub import AudioSegment, silence
import os
def process_mp3(filepath):
audio = AudioSegment.from_mp3(filepath)
# 無音区間を検出して削除
non_silent_audio = silence.detect_nonsilent(audio, min_silence_len=500, silence_thresh=-50)[0]
trimmed_audio = audio[non_silent_audio[0]:]
# 1秒の無音を作成
silence_segment = AudioSegment.silent(duration=1000)
# 無音を冒頭に追加
audio_with_silence = silence_segment + trimmed_audio
# トリミング
final_trimmed_audio = audio_with_silence[:15000]
# フェードアウト
faded_audio = final_trimmed_audio.fade_out(1000)
processed_filepath = filepath.replace('.mp3', '_intro.mp3')
faded_audio.export(processed_filepath, format='mp3')
return processed_filepath

2
web/requirements.txt Normal file
View File

@ -0,0 +1,2 @@
Flask
pydub

14
web/templates/index.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MP3 Uploader</title>
</head>
<body>
<h1>Upload MP3 Files</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="files" accept=".mp3" multiple>
<input type="submit" value="Upload">
</form>
</body>
</html>