Spaces:
Sleeping
Sleeping
Commit
·
626dde1
1
Parent(s):
eb78dc9
add files and update readme
Browse files- Dockerfile +29 -0
- LICENSE +21 -0
- README.md +10 -0
- app.py +96 -0
- font/Roboto-Black.ttf +0 -0
- fonts/Roboto-Black.ttf +0 -0
Dockerfile
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9-slim
|
2 |
+
|
3 |
+
# Install system packages
|
4 |
+
RUN apt-get update && apt-get install -y \
|
5 |
+
ffmpeg \
|
6 |
+
imagemagick \
|
7 |
+
fontconfig \
|
8 |
+
&& apt-get clean \
|
9 |
+
&& rm -rf /var/lib/apt/lists/*
|
10 |
+
|
11 |
+
# Install pip packages
|
12 |
+
RUN pip install gradio==3.50.2
|
13 |
+
|
14 |
+
# Set environment variables for matplotlib and fontconfig
|
15 |
+
ENV MPLCONFIGDIR=/tmp/matplotlib
|
16 |
+
ENV FONTCONFIG_PATH=/etc/fonts
|
17 |
+
|
18 |
+
# Create needed directories with correct permissions
|
19 |
+
RUN mkdir -p /tmp/matplotlib /usr/share/fonts /src/fonts /root/.config /src/flagged && \
|
20 |
+
chmod -R 777 /tmp /usr/share/fonts /root/.config /src/fonts /src/flagged
|
21 |
+
|
22 |
+
# Copy the rest of the application code
|
23 |
+
COPY . /src
|
24 |
+
|
25 |
+
# Set the working directory
|
26 |
+
WORKDIR /src
|
27 |
+
|
28 |
+
# Set the entry point to run the prediction script
|
29 |
+
ENTRYPOINT ["python", "app.py"]
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2024 Cortex Inc Ltd
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,3 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
## Waveform Video Generator
|
2 |
This project provides a web-based interface for generating waveform videos from audio files using Gradio. The application utilizes FFmpeg and ImageMagick to overlay captions and customize the appearance of the waveform. The environment is set up using a Docker container for ease of deployment.
|
3 |
|
|
|
1 |
+
---
|
2 |
+
title: Converts any audio or video to a waveform animation.
|
3 |
+
emoji: 🛠️
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: green
|
6 |
+
sdk: docker
|
7 |
+
app_file: app.py
|
8 |
+
pinned: false
|
9 |
+
---
|
10 |
+
|
11 |
## Waveform Video Generator
|
12 |
This project provides a web-based interface for generating waveform videos from audio files using Gradio. The application utilizes FFmpeg and ImageMagick to overlay captions and customize the appearance of the waveform. The environment is set up using a Docker container for ease of deployment.
|
13 |
|
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
import gradio as gr
|
3 |
+
import tempfile
|
4 |
+
from pathlib import Path
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Check permissions and environment setup
|
8 |
+
print("Checking directory and file permissions...")
|
9 |
+
for path in ['/tmp', '/usr/share/fonts', '/root/.config', '/src/fonts', '/src/flagged']:
|
10 |
+
if os.access(path, os.W_OK):
|
11 |
+
print(f'Directory {path} is writable.')
|
12 |
+
else:
|
13 |
+
print(f'Error: Directory {path} is not writable.')
|
14 |
+
|
15 |
+
class Predictor:
|
16 |
+
def predict(self,
|
17 |
+
audio: str,
|
18 |
+
bg_color: str = "#000000",
|
19 |
+
fg_alpha: float = 0.75,
|
20 |
+
bars_color: str = "#ffffff",
|
21 |
+
bar_count: int = 100,
|
22 |
+
bar_width: float = 0.4,
|
23 |
+
caption_text: str = "",
|
24 |
+
) -> str:
|
25 |
+
"""Make waveform video from audio file"""
|
26 |
+
waveform_video = gr.make_waveform(
|
27 |
+
audio,
|
28 |
+
bg_color=bg_color,
|
29 |
+
fg_alpha=fg_alpha,
|
30 |
+
bars_color=bars_color,
|
31 |
+
bar_count=bar_count,
|
32 |
+
bar_width=bar_width,
|
33 |
+
)
|
34 |
+
|
35 |
+
if caption_text == "" or caption_text is None:
|
36 |
+
return waveform_video
|
37 |
+
else:
|
38 |
+
padded_waveform_path = tempfile.mktemp(suffix=".mp4")
|
39 |
+
background_image_path = tempfile.mktemp(suffix=".png")
|
40 |
+
final_video_path = tempfile.mktemp(suffix=".mp4")
|
41 |
+
|
42 |
+
print("Running ffmpeg to add padding...")
|
43 |
+
subprocess.run([
|
44 |
+
'ffmpeg', '-y', '-i', waveform_video, '-vf',
|
45 |
+
f'pad=width=1000:height=667:x=0:y=467:color={bg_color[1:]}',
|
46 |
+
padded_waveform_path
|
47 |
+
], check=True)
|
48 |
+
|
49 |
+
print("Creating an image using ImageMagick...")
|
50 |
+
subprocess.run([
|
51 |
+
'convert', '-background', bg_color, '-fill', bars_color, '-font', '/src/fonts/Roboto-Black.ttf',
|
52 |
+
'-pointsize', '48', '-size', '900x367', '-gravity', 'center', f'caption:{caption_text}',
|
53 |
+
'-bordercolor', bg_color, '-border', '40', background_image_path
|
54 |
+
], check=True)
|
55 |
+
|
56 |
+
print("Overlaying the image on the padded waveform video...")
|
57 |
+
subprocess.run([
|
58 |
+
'ffmpeg', '-y', '-i', padded_waveform_path, '-i', background_image_path,
|
59 |
+
'-filter_complex', 'overlay=0:0', final_video_path
|
60 |
+
], check=True)
|
61 |
+
|
62 |
+
return final_video_path
|
63 |
+
|
64 |
+
# Gradio user interface
|
65 |
+
def gradio_predict(audio, bg_color, fg_alpha, bars_color, bar_count, bar_width, caption_text):
|
66 |
+
predictor = Predictor()
|
67 |
+
result = predictor.predict(
|
68 |
+
audio=audio,
|
69 |
+
bg_color=bg_color,
|
70 |
+
fg_alpha=fg_alpha,
|
71 |
+
bars_color=bars_color,
|
72 |
+
bar_count=bar_count,
|
73 |
+
bar_width=bar_width,
|
74 |
+
caption_text=caption_text
|
75 |
+
)
|
76 |
+
return result
|
77 |
+
|
78 |
+
# Launch Gradio interface
|
79 |
+
interface = gr.Interface(
|
80 |
+
fn=gradio_predict,
|
81 |
+
inputs=[
|
82 |
+
gr.Audio(source="upload", type="filepath", label="Audio File"),
|
83 |
+
gr.Textbox(value="#000000", label="Background Color"),
|
84 |
+
gr.Slider(0, 1, value=0.75, label="Foreground Opacity"),
|
85 |
+
gr.ColorPicker(value="#ffffff", label="Bars Color"),
|
86 |
+
gr.Slider(10, 500, value=100, step=1, label="Number of Bars"),
|
87 |
+
gr.Slider(0, 1, value=0.4, step=0.1, label="Bar Width"),
|
88 |
+
gr.Textbox(value="", label="Caption Text")
|
89 |
+
],
|
90 |
+
outputs=gr.Video(label="Waveform Video"),
|
91 |
+
live=False
|
92 |
+
)
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
print("Starting Gradio interface...")
|
96 |
+
interface.launch(server_name="0.0.0.0", server_port=7860)
|
font/Roboto-Black.ttf
ADDED
Binary file (168 kB). View file
|
|
fonts/Roboto-Black.ttf
ADDED
Binary file (168 kB). View file
|
|