Browse Source

Audio waveform and png are now generated from a temp dat file to avoid processing it twice

Dashie der otter 1 week ago
parent
commit
2ff614659f
Signed by: Dashie <dashie@sigpipe.me> GPG Key ID: C2D57B325840B755
3 changed files with 46 additions and 6 deletions
  1. 0
    2
      TODO.org
  2. 10
    3
      transcoding_utils.py
  3. 36
    1
      utils.py

+ 0
- 2
TODO.org View File

@@ -15,8 +15,6 @@
15 15
 ** SQL optimization
16 16
 ** translations check
17 17
 
18
-Generate a temp .dat file to generate .png and json to avoid dual processing of audio file
19
-
20 18
 ~~~~~~~~~ ACTIVITYPUB ~~~~~~~~
21 19
 -> profile UPDATE not handled yet (littleboxes don't handle UPDATE) upstream
22 20
 

+ 10
- 3
transcoding_utils.py View File

@@ -9,7 +9,7 @@ import magic
9 9
 import mutagen
10 10
 
11 11
 from models import db, SoundInfo, Sound
12
-from utils import get_waveform, create_png_waveform, duration_song_human, add_user_log
12
+from utils import get_waveform, create_png_waveform, duration_song_human, add_user_log, generate_audio_dat_file
13 13
 from pydub import AudioSegment
14 14
 from os.path import splitext
15 15
 from flask import current_app
@@ -203,8 +203,11 @@ def work_metadatas(sound_id, force=False):
203 203
         else:
204 204
             fname_t = fname
205 205
 
206
+        print("- GENERATING AUDIO DAT FILE")
207
+        dat_file_name = generate_audio_dat_file(fname_t)
208
+
206 209
         print("- WORKING WAVEFORM on {0}, {1}".format(sound.id, sound.filename))
207
-        waveform_infos = get_waveform_infos(fname_t)
210
+        waveform_infos = get_waveform_infos(dat_file_name)
208 211
         print("- Our file got waveform infos: {0}".format(waveform_infos))
209 212
         _infos.waveform = waveform_infos
210 213
         if not waveform_infos:
@@ -223,7 +226,11 @@ def work_metadatas(sound_id, force=False):
223 226
             if not os.path.isdir(fdir_wf):
224 227
                 os.makedirs(fdir_wf)
225 228
 
226
-            create_png_waveform(fname_t, fname_wf)
229
+            create_png_waveform(dat_file_name, fname_wf)
230
+
231
+        # Delete the temporary dat file
232
+        os.unlink(dat_file_name)
233
+
227 234
         _infos.done_waveform = True
228 235
 
229 236
     db.session.add(_infos)

+ 36
- 1
utils.py View File

@@ -119,13 +119,45 @@ def duration_song_human(seconds):
119 119
         return "%.2f sec" % seconds + "s" * (seconds != 1)
120 120
 
121 121
 
122
+def generate_audio_dat_file(filename):
123
+    binary = current_app.config["AUDIOWAVEFORM_BIN"]
124
+    if not os.path.exists(binary) or not os.path.exists(filename):
125
+        add_log("AUDIOWAVEFORM", "ERROR", "Filename {0} or binary {1} invalid".format(filename, binary))
126
+        return None
127
+
128
+    fname, _ = splitext(filename)
129
+
130
+    audio_dat = "{0}.dat".format(fname)
131
+
132
+    cmd = [binary, "-i", filename, "-o", audio_dat, "-b", "8"]
133
+
134
+    try:
135
+        process = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
136
+        if not process:
137
+            add_log("AUDIOWAVEFORM_DAT", "ERROR", "Subprocess returned None")
138
+            return None
139
+    except subprocess.CalledProcessError as e:
140
+        add_log("AUDIOWAVEFORM_DAT", "ERROR", "Process error: {0}".format(e))
141
+        return None
142
+
143
+    print("- Command ran with: {0}".format(process.args))
144
+
145
+    if process.stderr.startswith(b"Can't generate"):
146
+        add_log("AUDIOWAVEFORM_DAT", "ERROR", "Process error: {0}".format(process.stderr))
147
+        return None
148
+
149
+    return audio_dat
150
+
151
+
122 152
 def get_waveform(filename):
123 153
     binary = current_app.config["AUDIOWAVEFORM_BIN"]
124 154
     if not os.path.exists(binary) or not os.path.exists(filename):
125 155
         add_log("AUDIOWAVEFORM", "ERROR", "Filename {0} or binary {1} invalid".format(filename, binary))
126 156
         return None
127 157
 
128
-    tmpjson = "{0}.json".format(filename)
158
+    fname, _ = splitext(filename)
159
+
160
+    tmpjson = "{0}.json".format(fname)
129 161
 
130 162
     cmd = [binary, "-i", filename, "--pixels-per-second", "10", "-b", "8", "-o", tmpjson]
131 163
 
@@ -179,7 +211,10 @@ def create_png_waveform(fn_audio, fn_png):
179 211
         add_log("AUDIOWAVEFORM_PNG", "ERROR", "Filename {0} or binary {1} invalid".format(fn_audio, binary))
180 212
         return None
181 213
 
214
+    fname, _ = splitext(fn_audio)
215
+
182 216
     pngwf = "{0}.png".format(fn_png)
217
+
183 218
     cmd = [
184 219
         binary,
185 220
         "-i",

Loading…
Cancel
Save