Play audio-only file with subtitles, possibly use some still image as stub

Posted on

Problem :

Is it possible to make mplayer display subtitles while playing an audio-only file?

I’ve already tried:

mplayer filename.mp3 -sub filename.srt

but no luck – it just doesn’t creates a viewport as soon as it sees that there is no video. -vo x11 doesn’t help either.

I know that I can convert my audio files to video files, or use some subtitle editing software such as AegiSub just for playing my files; but neither of these ways is convenient for me because I have a lot of audio files.

Solution :

How can I play audio-only file with subtitles in mplayer?

If you’re willing to be flexible on using an image as a stub and get ffmpeg involved in generating a placeholder, it can be done.

  1. Create a blank file with ffmpeg:

    ffmpeg -t 1500 -s 1024x768 -f rawvideo -pix_fmt rgb24 -r 25 -i /dev/zero silence.mpeg
    

    (via ffmpeg-user)

    -s can be whatever size you want the window

    -t should be at least as long as your audio in seconds, or the subtitles stop playing!

  2. Play the blank video, while specifying what you really want to play and the subtitles using -audiofile and -sub, eg:

    mplayer silence.mpeg -sub subtitles.srt -audiofile audio.mp3
    
  3. (optional) Tweak the video file in 1. generated to be red, or blue, or a pattern, or whatever you’d like as a background.

See subtitles in action in mplayer:

demo of mplayer

Yes, I know you can’t hear the audio in a .gif, but it does work!

Alternative in VLC

If yo prefer (or don’t have access to ffmpeg), VLC can handle audio and subtitles, eg:

vlc audio.file --sub-file subtitle_file.srt

But note that you have to select a visualisation; as subtitles won’t display over album art.

Or you can do it from the interface:

demo

(credit to Chris Zabriskie for Prelude #4 from the Free Music Archive… not that you can hear it in the gif)

I have accomplished this same task with mpv in the past without needing the video file. Use:

mpv --no-audio-display --force-window --sub-file="<subfile>" "<inputfile>"

I was making simple SRT subs for each song; 3 or 4 lines of lyric per, like:

1
00:00:00,000 --> 00:00:00,000
Sample Text
Sample Text Here
Sample Text Here Again

Changing your start/stop times for how long to display, obviously. 🙂 Works great for karaoke from my laptop to this day.

If you don’t need an arbitrary image, just a solid black color, then there’s no need to create a dummy video file: you can just reinterpret /dev/zero:

mplayer /dev/zero -rawvideo format=rgb24:pal:fps=20 -demuxer rawvideo 
        -audiofile filename.mp3 -sub filename.srt

By removing format=rgb24: the background can even be made green [#008800] (along with saving some percentage of CPU usage).
However, implementing an arbitrary-colored background is kind of problematic.
For instance, one can do it by feeding the color bytes through a pipe:

perl -e 'for (;;) {syswrite(STDOUT, "x3Fx3Fx3Fx00" x (1024*1024*2))}' | 
  mplayer - -rawvideo format=rgb32:pal:fps=20 -demuxer rawvideo 
          -audiofile filename.mp3 -sub filename.srt

, but then the ability to rewind backwards is lost.
Another option is to use the geq video filter and specify a desired color in YUV space:

mplayer /dev/zero -rawvideo pal_fps=20 -demuxer rawvideo -vf geq=63:128:128 
        -audiofile filename.mp3 -sub filename.srt

This way, rewinding backwards is working, but the CPU usage of playing grows noticeably higher.

Leave a Reply

Your email address will not be published. Required fields are marked *