Real-time video analysis: cURL & FFmpeg

In this post, we explore a novel approach to analyze remote video data in real time by streaming content directly with cURL and processing it with FFmpeg. This technique lets you bypass local file storage, speeding up workflows and reducing disk I/O overhead. By piping data directly into FFmpeg, you can extract frames, analyze scene changes, or generate dynamic thumbnails as the video is being streamed.
Overview
When working with media, efficiency is key. Combining cURL and FFmpeg into a seamless pipeline allows you to fetch remote media data and process it on the fly. This guide assumes you're using FFmpeg 6.0+ and cURL 7.81.0+ for optimal compatibility and features.
Setting up the pipeline
Here's how to set up a pipeline that extracts frames from a remote video stream. This command fetches the video using cURL and pipes it to FFmpeg for processing:
curl -s "http://example.com/path/to/video.mp4" | \
ffmpeg -f mp4 -i pipe:0 \
-vf "fps=1" \
-frame_pts 1 \
-v error \
-stats \
thumbnail_%03d.jpg
Explanation
-f mp4
specifies the input format explicitlypipe:0
tells FFmpeg to read from standard input-frame_pts 1
enables presentation timestamp tracking-v error
shows only error messages-stats
displays progress information
Advanced uses
The basic pipeline can be extended for more complex tasks:
-
Scene Change Detection: Use FFmpeg's scene detection capabilities with proper thresholds:
curl -s "http://example.com/path/to/video.mp4" | \ ffmpeg -f mp4 -i pipe:0 \ -filter_complex "select='gt(scene,0.3)',metadata=print:file=time.txt" \ -vsync vfr \ -frame_pts 1 \ scene_%03d.jpg
This command detects scene changes with a 0.3 threshold and outputs frame timestamps to time.txt for further analysis.
-
Real-time Analysis: Process video streams with optimized settings:
curl -s "http://example.com/path/to/video.mp4" | \ ffmpeg -f mp4 -i pipe:0 \ -vf "scale=640:-1,fps=5" \ -max_muxing_queue_size 1024 \ -v warning \ -f image2pipe \ -vcodec ppm \ pipe:1
This example scales the video, reduces the frame rate for processing, and outputs raw frames for real-time analysis.
Troubleshooting
Common issues and solutions:
-
Network Issues: Add retry logic to cURL:
curl --retry 3 --retry-delay 2 -s "http://example.com/path/to/video.mp4"
-
Buffer Overflows: Adjust FFmpeg's buffer settings:
-max_muxing_queue_size 1024 -thread_queue_size 512
-
Format Detection: Explicitly specify input format if auto-detection fails:
ffmpeg -f mp4 -i pipe:0 ...
Performance considerations
For optimal performance:
- Scale down video resolution for analysis when full resolution isn't needed
- Use appropriate frame rates for your use case
- Consider hardware acceleration where available
- Monitor system resources and adjust parameters accordingly
Note that real-time analysis capabilities depend on input format, processing power, and network bandwidth. For high-resolution streams, consider reducing the processing load by lowering the analysis framerate or using a scaled-down version of the input.
Conclusion
The combination of cURL and FFmpeg provides a powerful toolkit for real-time media processing. This approach offers flexibility and efficiency for various video analysis tasks. At Transloadit, we use similar techniques in our media processing pipeline to deliver fast and reliable results.