Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've been experimenting with streaming via FFMpeg. I love how powerful it is! Currently I have it set up so that it streams my screen and adds an overlay for my webcam. Though, there is one thing that I'd like to be able to do that I just can't quite figure out: Throw up an on-demand splash screen.

Perhaps this would be better explained via example. Say you're streaming a game or something via Twitch for a long while. Then, you have to step away. You could

  1. Just walk away, or
  2. Throw up a "Be right back!" splash screen or something, perhaps with a little music (I've got that part figured out thanks to the awesomeness of Pulseaudio)

I figured the best way to accomplish this was via a second overlay that overlays the screen/webcam overlay.

I attempted to add a third input akin to this: ffmpeg -f v4l2 -i /dev/video0 -f xllgrab -i :0.0+738,0 -i ~/splashscreen.png -loop 1 -fliter_complex "[0:1][0:0]overlay=0:0[out];[out][2:0]overlay=0:0[final]" -f flv ~/Videos/test.flv (requires a newer version of ffmpeg than is in the Saucy repos)

And it worked, as the splash screen was transparent. I thought I'd do something clever and swap in a non-transparent image when I needed the splash screen but either ffmpeg caches the looped image or it ignores it when it suddenly can't find the file for a brief instance.

The second way I thought of was perhaps using a named pipe and using another ffmpeg instance to pipe in they overlay splash screen when I need it, but from what I've read, if ffmpeg doesn't find any data in the pipe it will not continue processing the rest of the video, instead it will only put out frames when it can pull from all the input sources.

If there are any ffmpeg gurus out there, I'd love to hear some ideas!

share|improve this question
    
Since x11grab will show whatever is on your screen why not just switch the focus in your windows manager (alt+tab or similar) to a fullscreen image whenever you need to use the shitter? I don't think the image2 demuxer has the -update option like the muxer does...might be a good feature request. Also, I highly recommend looking at Encoding for streaming sites with ffmpeg to optimize your stream for your, or your viewers, network speed (includes some examples too). –  LordNeckbeard Mar 24 at 17:59
    
I've already been through that article, thanks though =). The reason that won't necessarily work is because I'll be streaming fullscreen games via Twitch and due to some Wine issues, it's a little finicky at best. –  Githlar Mar 24 at 20:29
    
Only other idea I have, excluding writing a patch to add -update functionality for the demuxer, is to simply stop the recording and then initiate a new ffmpeg command that just shows splashscreen.png. I don't know if that will work on Twitch or not since I'm not really familiar with the site. –  LordNeckbeard Mar 24 at 22:30
    
Yeah, it won't necessarily work with Twitch since it wouldn't be a graceful thing. It would kick the users off and then after a certain timeout it would load the video again, sync, rebuffer. It's just clunky. –  Githlar Mar 26 at 7:46
add comment

1 Answer 1

up vote 2 down vote accepted

I have done it! Granted, it required some backflips, but none-the-less it works =D

Here is the script I used:

#!/bin/bash

#NOTE: The framerate throttling can probably be removed due to the limit in
#the amount of data that can be written to a pipe before it blocks. My
#streams are at 1920x1080 and cat only puts 5 frames onto the pipe
#before it hangs waiting for a program to tread the pipe. I found this odd
#since my splash screen images were only 8k, whereas the pipe size is 1M.
#Therefore, it might be highly dependent on the resolution of your splash
#images. By removing the FPS logic of this script, a higher potential
#framerate can be achieved by the script if that's necessary.

#This is my third FFMpeg input. My other two are at framerates of
#10 (screen) and 30 (webcam). My output is 10 FPS. I had originally
#set this to 10 as well, but it caused FFMpeg to choke and start dropping
#frames for some reason, so I just added 5 since it's a common denominator
#of both 10 and 30. +2 may have also worked, but that seemed a little too
#close for me since even 11 had issues with output framerate, hovering
#around 9.9 FPS rather than the target 10 FPS.

framerate=15

#Tracks how many frames we've written this second
files_this_second=0
this_second=$(date +%s)

while [ 1 ]
do
  #Haven't written all the frames for this second
  if [ $files_this_second -lt $framerate ]
  then
    if [ ! -f /file/to/test/to/throw/up/splash ]
    then
      #Output the transparent "splash screen" (no splash screen)
      cat /path/to/a/transparent.png
    else
      #Output the splash screen
      cat /path/to/a/splashscreen.png
    fi
    files_this_second=$(($files_this_second + 1))
  else
    #The second has changed
    if [ "$(date +%s)" != "$this_second" ]
    then
      this_second=$(date +%s)
      files_this_second=0
    fi
  fi
done

I can then feed the output of this script into FFMpeg like so:

splash.sh | ffmpeg -f image2pipe -vcodec png -framerate 15 -i - (some other video source and parameters) -filter_complex "[1:0]setpts=PTS-STARTPTS[video];[0:0]setpts=PTS-STARTPTS[splashscreen];[video][splashscreen]overlay=0:0" (output format options and file)

Then, I set up a hotkey to run another simple script:

#!/bin/bash

if [ -f /file/to/test/to/throw/up/splash ]
then
  rm /file/to/test/to/throw/up/splash
else
  touch /file/to/test/to/throw/up/splash
fi

When I want to throw up the splash screen, I hit the hotkey. When I want to take it down I hit the hotkey again.

This is useful for a couple of things:

  1. A splash screen (as mentioned)
  2. Blanking the screen when entering passwords or something

It also bypasses the problem of the images getting cached like in my question above, so you'd simply have to replace the image with another to set another splash screen.

I think it's brilliant! What do you guys think?

NOTE: I am using a self-compiled version of FFMpeg because the one available in the Saucy repositories is pretty outdated. Also, the image2pipe format is undocumented that I can find.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.