Using FFmpeg Playback with Read PCM Data Pipeline: No Data Available - Instead, Continuing Seeking
FFmpeg is a powerful multimedia framework capable of handling various audio and video codecs. In some scenarios, we might want to process audio streams using FFmpeg and read raw PCM data in real-time. However, when dealing with streaming content, we may encounter situations where no data is available, and the pipeline continues seeking. In this article, we will discuss how to handle such cases using FFmpeg and Python.
Prerequisites
To follow this tutorial, ensure you have the following:
- FFmpeg installed:
https://ffmpeg.org/download.html - Python 3.x installed:
https://www.python.org/downloads/
FFmpeg Playback with Read PCM Data Pipeline
First, let's create a simple FFmpeg pipeline to play an audio file and read raw PCM data using the av_read_frame() function:
Step 1: Create a Python script to read PCM data from FFmpeg
ffmpeg_pcmdataread.py
0:
data = packet.data
size = packet.size
format = av_get_sample_format(avformat_context.streams[0].codecpar.format)
if format == AV_SAMPLE_FMT_S16:
pcm_data = struct.unpack_from('<' + str(size // 2) + 'h', data, 0)
return pcm_data
else:
print("Unsupported PCM format.")
return None
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python ffmpeg_pcmdataread.py ")
sys.exit()
input_file = sys.argv[1]
pcm_data = read_pcm_data(input_file)
if pcm_data is not None:
for sample in pcm_data:
print(f"Sample: {sample}")
else:
print("No data available.")
Step 2: Play the audio file using FFmpeg
Use the following command to play the audio file:
This command plays the input.mp3 file and writes the raw PCM data to stdout. We will use this pipeline in our Python script to read the PCM data.
Handling No Data Available Cases
When dealing with streaming content, you may encounter situations where no data is available, and the pipeline continues seeking. In such cases, we can modify our Python script to continue seeking and reading data when no packets are available:
0:
data = packet.data
size = packet.size
format = av_get_sample_format(avformat_context.streams[0].codecpar.format)
if format == AV_SAMPLE_FMT_S16:
pcm_data = struct.unpack_from('<' + str(size // 2) + 'h', data, 0)
return pcm_data
if last_packet_pts + last_packet_size == avformat_context.streams[0].ptstime:
time.sleep(0.01)
else:
last_packet_pts = avformat_context.streams[0].ptstime
last_packet_size = packet.size
return None
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python ffmpeg_pcmdataread.py ")
sys.exit()
input_file = sys.argv[1]
pcm_data = read_pcm_data(input_file)
if pcm_data is not None:
for sample in pcm_data:
print(f"Sample: {sample}")
else:
print("No data available. Continuing seeking...")
time.sleep(0.1)
read_pcm_data(input_file)
In this article, we discussed how to handle situations where no data is available when using FFmpeg to play audio files and read raw PCM data using the av_read_frame() function. We created a Python script that reads raw PCM data from FFmpeg and demonstrated how to modify the script to continue seeking and reading data when no packets are available.
References