在TensorFlow中捕获FFmpeg错误的方法咨询
Great question! TensorFlow's FFmpeg-based audio decoding runs as part of the computation graph, so standard Python try/except blocks won't catch errors when you define the graph—you need to handle them either during graph execution or by adding validation nodes to your graph. Here are a few practical approaches tailored to your code:
1. Catch TensorFlow Exceptions During Session Execution
The most straightforward way is to wrap your session.run() call in a try/except block. When FFmpeg fails to decode the MP3 (e.g., corrupted file, wrong format), TensorFlow will throw an InvalidArgumentError (or other relevant errors like NotFoundError for missing files).
Here's how to adjust your usage:
import tensorflow as tf from tensorflow.contrib import ffmpeg def load(mp3_path): with tf.name_scope("loading") as scope: audio_binary = tf.read_file(mp3_path) waveform = tf.reshape(ffmpeg.decode_audio( audio_binary, file_format='mp3', samples_per_second=44100, channel_count=1), [-1]) return waveform # When using the load function try: with tf.Session() as sess: audio_wave = sess.run(load("your_audio.mp3")) print(f"Audio loaded successfully! Length: {len(audio_wave)} samples") except tf.errors.InvalidArgumentError as e: print(f"FFmpeg Decoding Error: {e}") # Add custom handling here (e.g., log the failure, skip the file) except tf.errors.NotFoundError as e: print(f"File Error: {e}")
2. Add Validation Nodes to Your Computation Graph
You can embed checks directly into your graph to catch invalid outputs early. For example, you can verify the decoded waveform isn't empty, or check for NaN/Inf values that might indicate a decoding failure:
def load(mp3_path): with tf.name_scope("loading") as scope: audio_binary = tf.read_file(mp3_path) waveform = tf.reshape(ffmpeg.decode_audio( audio_binary, file_format='mp3', samples_per_second=44100, channel_count=1), [-1]) # Check if the waveform is empty (common sign of decoding failure) waveform = tf.cond( tf.equal(tf.size(waveform), 0), lambda: tf.constant([], dtype=tf.float32, name="empty_decoding_error"), lambda: waveform ) # Check for invalid numerical values (NaN/Inf) waveform = tf.debugging.check_numerics(waveform, "Decoded audio contains invalid values (NaN/Inf)") return waveform
When you run this graph, these checks will trigger explicit errors if something goes wrong during decoding.
3. Use tf.py_func for Custom Python Validation
If you want more control over error messaging or logic, you can use tf.py_func to inject a Python validation function into the graph. This lets you raise custom exceptions based on the decoded output:
def validate_waveform(waveform): if len(waveform) == 0: raise ValueError("FFmpeg failed to decode the MP3: empty audio output") return waveform def load(mp3_path): with tf.name_scope("loading") as scope: audio_binary = tf.read_file(mp3_path) waveform = tf.reshape(ffmpeg.decode_audio( audio_binary, file_format='mp3', samples_per_second=44100, channel_count=1), [-1]) # Add custom validation via tf.py_func waveform = tf.py_func(validate_waveform, [waveform], tf.float32) waveform.set_shape([None]) # Preserve shape information for downstream operations return waveform
Quick Note on TensorFlow Versions
Keep in mind that tensorflow.contrib.ffmpeg is deprecated in newer TensorFlow versions (especially TF 2.x). If you ever migrate to TF 2.x, consider using a standalone FFmpeg Python library to decode audio to a NumPy array first—this gives you full access to Python's standard error handling before passing the data to TensorFlow.
内容的提问来源于stack exchange,提问作者lollercoaster

