Right now when PALE displays a video, it just does that. There are two ways to go about adding audio:
- "Cheap Way": Just add a few more flags to
ffmpegto make it stream the sound to the default audio output of the system and call it a day. I have tried this, and one can try this with the following patch on GNU/Linux systems where Pulseaudio (through Pipewire or otherwise) is used:
diff --git a/pale-video.c b/pale-video.c
index f4743d1..b4004d7 100644
--- a/pale-video.c
+++ b/pale-video.c
@@ -296,10 +296,16 @@ pale_video_create(const char *video_path, PaleImage *target_image)
snprintf(size_str, sizeof(size_str), "%dx%d", player->width,
player->height);
- execlp("ffmpeg", "ffmpeg", "-i", video_path, "-vf", "vflip",
- "-f", "rawvideo", "-pix_fmt", "rgb24", "-s",
- size_str, /* Scale to target size */
- "-", NULL);
+ execlp("ffmpeg", "ffmpeg",
+ "-re", /* Read at native frame rate for sync */
+ "-i", video_path,
+
+ /* video to pipe */
+ "-map", "0:v", "-vf", "vflip", "-f", "rawvideo",
+ "-pix_fmt", "rgb24", "-s", size_str, "-",
+
+ /* audio to system speakers */
+ "-map", "0:a", "-f", "pulse", "default", NULL);
/* If exec fails */
_exit(1);
You can replace the pulse with alsa if you don't use the former. The issue with this is that the audio is noticeably out of sync with the frames of the video. I have attached a demo that reproduces this. Also to be noted that this seems cheap way also seems to introduce slight lags in the video frames as well.
- Proper AV Sync: This is the actually efficient way to implement this. It would entail tracking a Master Clock, usually the audio clock is considered the Master and then we try to make sure the video frames are aligned according to it. Also we'd need to output the audio from
ffmpegin some interleaved(?) format to receive it so we'll need mutlipe pipes that do bothaudioandvideo. And then we need to process the audio samples through another dynamically linked audio library and then do the syncing.
Obviously we need to do the second approach, but the choice of audio library, the clock tracking architecture and other details need to be discussed.