Krokodilpirat commited on
Commit
9039102
·
verified ·
1 Parent(s): 2d19f38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -76
app.py CHANGED
@@ -164,7 +164,12 @@ def infer_video_depth(
164
 
165
  # Nachdem die Videos erstellt wurden, wenden wir den Loop-Faktor an
166
  if loop_factor > 1:
 
 
 
 
167
  depth_looped_path = os.path.join(output_dir, os.path.splitext(os.path.basename(depth_vis_path))[0] + f'_loop{loop_factor}.mp4')
 
168
 
169
  # Erstelle eine temporäre Textdatei mit der Liste der zu wiederholenden Dateien
170
  concat_file_path = os.path.join(output_dir, 'concat_list.txt')
@@ -184,13 +189,22 @@ def infer_video_depth(
184
  ]
185
  subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
186
 
187
- # Ersetze den ursprünglichen Pfad durch den neuen geloopten Pfad
188
- depth_vis_path = depth_looped_path
 
 
 
 
189
 
190
  if stitch and stitched_video_path:
191
- # Speichern wir den Originalnamen
192
  original_path = stitched_video_path
193
 
 
 
 
 
 
194
  # Überprüfen wir, ob das Input-Video einen Audio-Stream hat
195
  has_audio = False
196
  check_audio_cmd = [
@@ -204,92 +218,135 @@ def infer_video_depth(
204
  stderr = result.stderr.decode('utf-8')
205
  if "Audio" in stderr:
206
  has_audio = True
207
-
208
- # Erstelle eine temporäre Textdatei für die stitched Videos
209
- concat_stitched_file_path = os.path.join(output_dir, 'concat_stitched_list.txt')
210
- with open(concat_stitched_file_path, 'w') as f:
211
- for _ in range(loop_factor):
212
- f.write(f"file '{original_path}'\n")
213
 
214
  # Temporärer Pfad für das geloopte Video ohne Audio
215
- temp_looped_path = os.path.join(output_dir, 'temp_looped_rgbd.mp4')
216
 
217
- # Verwende ffmpeg, um das Video zu loopen
218
- concat_cmd = [
219
- "ffmpeg",
220
- "-y",
221
- "-f", "concat",
222
- "-safe", "0",
223
- "-i", concat_stitched_file_path,
224
- "-c", "copy",
225
- temp_looped_path
226
- ]
227
- subprocess.run(concat_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
228
-
229
- # Wenn Audio vorhanden ist, müssen wir es separat behandeln
230
- if has_audio:
231
- # Extrahiere den Audio-Track aus dem originalen Input-Video
232
- # Dies ist die sauberste Quelle
233
- audio_path = os.path.join(output_dir, 'extracted_audio.aac')
234
- extract_audio_cmd = [
235
- "ffmpeg",
236
- "-y",
237
- "-i", input_video, # Original Input-Video verwenden
238
- "-vn", "-acodec", "copy",
239
- audio_path
240
- ]
241
- subprocess.run(extract_audio_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
242
-
243
- # Erstelle eine Textdatei für das Audio-Looping
244
- concat_audio_file_path = os.path.join(output_dir, 'concat_audio_list.txt')
245
- with open(concat_audio_file_path, 'w') as f:
246
  for _ in range(loop_factor):
247
- f.write(f"file '{audio_path}'\n")
 
 
248
 
249
- # Erstelle den geloopten Audio-Track
250
- looped_audio_path = os.path.join(output_dir, 'looped_audio.aac')
251
- audio_loop_cmd = [
252
  "ffmpeg",
253
  "-y",
254
  "-f", "concat",
255
  "-safe", "0",
256
- "-i", concat_audio_file_path,
257
  "-c", "copy",
258
- looped_audio_path
259
  ]
260
- subprocess.run(audio_loop_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
 
 
261
 
262
- # Kombiniere das geloopte Video mit dem geloopten Audio
263
- final_cmd = [
264
- "ffmpeg",
265
- "-y",
266
- "-i", temp_looped_path,
267
- "-i", looped_audio_path,
268
- "-c:v", "copy",
269
- "-c:a", "aac",
270
- "-map", "0:v:0",
271
- "-map", "1:a:0",
272
- "-shortest",
273
- original_path # Verwenden des originalen Pfads als Ziel
274
- ]
275
- subprocess.run(final_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
276
- else:
277
- # Wenn kein Audio vorhanden ist, einfach das Video umbenennen
278
- os.replace(temp_looped_path, original_path)
279
-
280
- # Bereinige temporäre Dateien
281
- temp_files = [concat_file_path, concat_stitched_file_path]
282
- if has_audio:
283
- temp_files.extend([concat_audio_file_path, audio_path, looped_audio_path])
284
- if os.path.exists(temp_looped_path):
285
- temp_files.append(temp_looped_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
 
287
- for file_path in temp_files:
288
- if os.path.exists(file_path):
289
- try:
290
- os.remove(file_path)
291
- except:
292
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
 
294
  gc.collect()
295
  torch.cuda.empty_cache()
 
164
 
165
  # Nachdem die Videos erstellt wurden, wenden wir den Loop-Faktor an
166
  if loop_factor > 1:
167
+ # Stellen sicher, dass das Ausgabeverzeichnis existiert
168
+ os.makedirs(output_dir, exist_ok=True)
169
+
170
+ # Für die Tiefenkarte
171
  depth_looped_path = os.path.join(output_dir, os.path.splitext(os.path.basename(depth_vis_path))[0] + f'_loop{loop_factor}.mp4')
172
+ print(f"Creating looped depth video with factor {loop_factor}")
173
 
174
  # Erstelle eine temporäre Textdatei mit der Liste der zu wiederholenden Dateien
175
  concat_file_path = os.path.join(output_dir, 'concat_list.txt')
 
189
  ]
190
  subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
191
 
192
+ # Überprüfe, ob die Datei erstellt wurde
193
+ if os.path.exists(depth_looped_path):
194
+ # Ersetze den ursprünglichen Pfad durch den neuen geloopten Pfad
195
+ depth_vis_path = depth_looped_path
196
+ else:
197
+ print(f"WARNING: Failed to create looped depth video at {depth_looped_path}")
198
 
199
  if stitch and stitched_video_path:
200
+ # Speichern wir den Originalnamen und absoluten Pfad
201
  original_path = stitched_video_path
202
 
203
+ print(f"Looping video {original_path} with factor {loop_factor}")
204
+
205
+ # Pfad für das Ausgangsverzeichnis sicherstellen
206
+ output_folder = os.path.dirname(original_path)
207
+
208
  # Überprüfen wir, ob das Input-Video einen Audio-Stream hat
209
  has_audio = False
210
  check_audio_cmd = [
 
218
  stderr = result.stderr.decode('utf-8')
219
  if "Audio" in stderr:
220
  has_audio = True
221
+ print("Audio stream detected in input video")
 
 
 
 
 
222
 
223
  # Temporärer Pfad für das geloopte Video ohne Audio
224
+ temp_looped_path = os.path.join(output_folder, 'temp_looped_rgbd.mp4')
225
 
226
+ try:
227
+ # Erstelle eine temporäre Textdatei für die stitched Videos
228
+ concat_stitched_file_path = os.path.join(output_folder, 'concat_stitched_list.txt')
229
+ with open(concat_stitched_file_path, 'w') as f:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  for _ in range(loop_factor):
231
+ f.write(f"file '{original_path}'\n")
232
+
233
+ print(f"Creating temporary file at: {temp_looped_path}")
234
 
235
+ # Verwende ffmpeg, um das Video zu loopen
236
+ concat_cmd = [
 
237
  "ffmpeg",
238
  "-y",
239
  "-f", "concat",
240
  "-safe", "0",
241
+ "-i", concat_stitched_file_path,
242
  "-c", "copy",
243
+ temp_looped_path
244
  ]
245
+ process = subprocess.run(concat_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
246
+ print(f"FFmpeg concat command exit code: {process.returncode}")
247
+ if process.returncode != 0:
248
+ print(f"FFmpeg error: {process.stderr.decode('utf-8')}")
249
 
250
+ # Überprüfe, ob die temporäre Datei erzeugt wurde
251
+ if not os.path.exists(temp_looped_path):
252
+ print(f"ERROR: Failed to create temporary file {temp_looped_path}")
253
+ print(f"Current directory contents: {os.listdir(output_folder)}")
254
+ # Fallback
255
+ return [depth_vis_path, stitched_video_path]
256
+
257
+ # Wenn Audio vorhanden ist, müssen wir es separat behandeln
258
+ if has_audio:
259
+ # Extrahiere den Audio-Track aus dem originalen Input-Video
260
+ audio_path = os.path.join(output_folder, 'extracted_audio.aac')
261
+ extract_audio_cmd = [
262
+ "ffmpeg",
263
+ "-y",
264
+ "-i", input_video, # Original Input-Video verwenden
265
+ "-vn", "-acodec", "copy",
266
+ audio_path
267
+ ]
268
+ subprocess.run(extract_audio_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
269
+
270
+ # Prüfen, ob Audio extrahiert wurde
271
+ if not os.path.exists(audio_path) or os.path.getsize(audio_path) == 0:
272
+ print(f"WARNING: Failed to extract audio or no audio track in {input_video}")
273
+ has_audio = False
274
+ else:
275
+ # Erstelle eine Textdatei für das Audio-Looping
276
+ concat_audio_file_path = os.path.join(output_folder, 'concat_audio_list.txt')
277
+ with open(concat_audio_file_path, 'w') as f:
278
+ for _ in range(loop_factor):
279
+ f.write(f"file '{audio_path}'\n")
280
+
281
+ # Erstelle den geloopten Audio-Track
282
+ looped_audio_path = os.path.join(output_folder, 'looped_audio.aac')
283
+ audio_loop_cmd = [
284
+ "ffmpeg",
285
+ "-y",
286
+ "-f", "concat",
287
+ "-safe", "0",
288
+ "-i", concat_audio_file_path,
289
+ "-c", "copy",
290
+ looped_audio_path
291
+ ]
292
+ subprocess.run(audio_loop_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
293
+
294
+ # Prüfe, ob Audio geloopt wurde
295
+ if not os.path.exists(looped_audio_path) or os.path.getsize(looped_audio_path) == 0:
296
+ print(f"WARNING: Failed to create looped audio")
297
+ has_audio = False
298
 
299
+ # Finaler Schritt: Kombiniere Video und Audio wenn nötig, sonst nur Video kopieren
300
+ if has_audio:
301
+ # Kombiniere das geloopte Video mit dem geloopten Audio
302
+ final_cmd = [
303
+ "ffmpeg",
304
+ "-y",
305
+ "-i", temp_looped_path,
306
+ "-i", looped_audio_path,
307
+ "-c:v", "copy",
308
+ "-c:a", "aac",
309
+ "-map", "0:v:0",
310
+ "-map", "1:a:0",
311
+ "-shortest",
312
+ original_path # Verwenden des originalen Pfads als Ziel
313
+ ]
314
+ subprocess.run(final_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
315
+ else:
316
+ # Wenn kein Audio vorhanden ist, einfach das Video kopieren
317
+ copy_cmd = [
318
+ "ffmpeg",
319
+ "-y",
320
+ "-i", temp_looped_path,
321
+ "-c", "copy",
322
+ original_path
323
+ ]
324
+ subprocess.run(copy_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
325
+
326
+ # Überprüfen, ob die Ersetzung erfolgreich war
327
+ if not os.path.exists(original_path):
328
+ print(f"ERROR: Failed to replace {original_path} with looped version")
329
+ else:
330
+ print(f"Successfully replaced {original_path} with looped version")
331
+
332
+ # Bereinige temporäre Dateien
333
+ temp_files = [concat_file_path, concat_stitched_file_path]
334
+ if has_audio:
335
+ temp_files.extend([concat_audio_file_path, audio_path, looped_audio_path])
336
+ if os.path.exists(temp_looped_path):
337
+ temp_files.append(temp_looped_path)
338
+
339
+ for file_path in temp_files:
340
+ if os.path.exists(file_path):
341
+ try:
342
+ os.remove(file_path)
343
+ except Exception as e:
344
+ print(f"Warning: Could not remove temporary file {file_path}: {str(e)}")
345
+
346
+ except Exception as e:
347
+ print(f"Error during looping process: {str(e)}")
348
+ # Im Fehlerfall die ursprünglichen Dateien behalten
349
+ return [depth_vis_path, stitched_video_path]
350
 
351
  gc.collect()
352
  torch.cuda.empty_cache()