As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label FFmpeg. Show all posts

Quite often, I use mogrify from ImageMagick and gifsicle to generate an animated GIF from a series of screen grabbed PNG images as listed below, such as in this post. I can never recall what options to use, especially for -format part.

1   Basic

1.1   From images

mogrify -format gif input-*.png
gifsicle -d100 -O3 -k256 -l0 input-*.gif > output.gif

-d delays 100*100thseconds is 1 second, -o optimization level 3, -k 256 colors, and -l loop forever.

In that post, I had to rotate the image for posting using convert, also from ImageMagick.

1.2   Using FFmpeg

With FFmpeg, it can directly output as GIF format, however, it will use dithering and that most likely wouldnt look good if you are recording a terminal window, furthermore, it will increase the file size significantly.

https://lh3.googleusercontent.com/-VgF2yfYtbXg/VltpCZxzZ3I/AAAAAAAAIhk/DmljIENMUJo/s800-Ic42/256.png

High quality GIF with FFmpeg explains how to generate and use a palette. In a terminal that use standard 16-color or 256-color, you can record using the following command with this palette image on the right:

ffmpeg -f x11grab -show_region 1 -r $FPS -s ${WIDTH}x${HEIGHT} \
       -i :0.0+${X},${Y} -i 256.png \
       -lavfi 'copy [x]; [x][1:v] paletteuse=dither=none' \
       output.raw.gif

The key point is the dither=none option of paletteuse filter, that will stop FFmpeg from trying to dither.

Note

paletteuse is only added since FFmpeg 2.6 (2015-03-06).

2   Trimming

If you need to trim out some frames, you can use the command as follows, which trims out the first frame (#0) and anything after the sixteenth (#15).

gifsicle output.gif '#1-15' > output.trim.gif

There is also a --delete options, it could be used like:

gifsicle output.gif --delete '#0' '#16-' > output.trim.gif

3   Rotation

gifsicle --rotate-90 output.gif > output.90.gif

There is also --rotate-180 and --rotate-270. You can also use convert from ImageMagick to do the task as seen below.

convert output.gif -rotate -90 output-90.gif

-rotate takes degree in clockwise rotation, therefore -90 is counter-clockwise for 90.

4   Last frame delay

To change last frame delay, so it wouldnt restart too fast if short delay is used in generating the GIF, you can do the following to change last frame delay to 5 seconds:

gifsicle -b output.gif -d100 '#0--2' -d500 '#-1'

# or simply

gifsicle -b output.gif -d500 '#-1'

#0--2 selects the frames between the first frame and the second last frame, and #-1 the last frame. For frame number reference, read Frame Selections in gifsicle(1).

Last year, my patch got into FFmpeg repository, just now, after FFmpeg 0.12.2 marked as stable after 0.7.8, I saw my contribution (from FFmpeg manpage):


Unfortunately, I didn't carry on after I created x11grabr. The issue I has still remained and I didn't try to resolve it very hard. Since then nothing was done, it was sort of ceased of development. When I started x11grabr, I wished I could make it like XSplit on Linux. Despite issues, my computer couldn't handle much encoding let along I don't really have anything interesting for live broadcasting.

If you are interested, please send me an email, maybe we can team up.

I have been broadcasting my desktop for a couple of days, mainly because I would need to test my x11grabr. I recalled more than a year ago, I would need to set up things in order to get streaming out. ( You might be seeing a new box showing up at the end of this box, which would be telling you click on the play button! )

1   Justin.tv

Note

Justin.tv was shut down on August 5, 2014.

On Justin.tv you can grab the stream key in channel info page1, scroll down for Stream Key.

I am not going to bore you, so here is the script I am currently using:

#!/bin/bash
# 2011-09-06T06:45:42Z

VIDEO_SIZE="630x352"
FPS="15"

STREAM_KEY="<INSERT YOU KEY HERE>"
STREAM_URL="rtmp://live.justin.tv/app/$STREAM_KEY"


/path/to/x11grabr/x11grabr -s "$VIDEO_SIZE" -r "$FPS" -bnone |
ffmpeg -f rawvideo -pix_fmt bgra -s "$VIDEO_SIZE" -r "$FPS" -i - \
       -f alsa -ac 1 -ar 11025 -i pulse \
       -vcodec libx264 -vpre baseline \
       -acodec libmp3lame -ab 32k \
       -threads 0 \
       -f flv "$STREAM_URL"
       #-f alsa -ac 2 -ar 22050 -i pulse \
       #-acodec pcm_s16le \
       #-acodec libmp3lame -ab 64k \
       #-acodec aac -strict experimental -ab 64k \
       #-acodec aac -strict experimental \

I use 630x352 because it gives me pixel-to-pixel when viewing on Justin.tv. I broadcast my desktop, so I dont like the image to be re-scaled. Also, you might want to use built-in x11grab in ffmpeg. So you may need to launch ffmpeg like (untested):

ffmpeg -f x11grab -pix_fmt bgra -s "$VIDEO_SIZE" -r "$FPS" -i :0.0 \
       # the rest of stuff

Note that I have submitted two patches, once they get released you would have better control.

1.1   IRC Chat

Justin.tvs webchat is pretty buggy, at least it doesnt like me much. I have seen message being eaten up, I thought why the other person kept talking to self. Another issue was funny, I had to turn off (uncheck) in order to show timestamp. But it seems fixed, right now.

If you use irssi, here is the simple configuration I have added:

servers = (
  {
    address = "livibetter.jtvirc.com";
    chatnet = "jtvme";
    port = "6667";
    password = "<YOUR JTV PASSWORD>";
    use_ssl = "no";
    ssl_verify = "no";
  }
);

chatnets = {
  jtvme = { type = "IRC"; };
};

channels = (
  { name = "#livibetter"; chatnet = "jtvme"; autojoin = "yes"; },
);

When I start broadcasting, I fire up irssi and issue /connect jtvme to connect to my own IRC channel.

If you dont have IRC client, currently stable Firefox allows you to open IRC via third-party website, its ehh, I forgot the name. Anyway, it has no problem to connect to JTVs IRC server. It took me sometime to Google correct information and pieced them up into my configuration.

1.2   Music

Streaming with music is always some vague issues. I love watching StarCraft II [SCII] channels and I have seen some complaints over some channels before, but right now it seems fine because those top-page popular channels play music via GrooveShark or YouTube. I dont know if its somewhat ignored by JTV. I dont believe those labels would let JTV off the hook.

At first, I didnt even have audio stream sent to JTV. I found its boring, so I started to play those music I downloaded from Jamendo before. I still didnt give them proper attribution, though I have put a Last.fm recently played on channel page.

[SCII]Nope, I dont play, I just watch. I use Linux.

2   Ustream

As for Ustream, you need to download the FME XML file, search for <stream>, the string inside is your STREAM_KEY.

#!/bin/bash

# Go Live -> Start Broadcasting

VIDEO_SIZE="hd480"
FPS="15"

STREAM_KEY="<INSERT YOU KEY HERE>"
# flashver part is important
STREAM_URL="rtmp://1.486020.fme.ustream.tv/ustreamVideo/486020/$STREAM_KEY flashver=FMLE/3.0\20(compatible;\20FMSc/1.0)"

/path/to/x11grabr/x11grabr -s "$VIDEO_SIZE" -r "$FPS" -bnone |
ffmpeg -f rawvideo -pix_fmt bgra -s "$VIDEO_SIZE" -r "$FPS" -i - \
       -f alsa -ac 2 -ar 22050 -i pulse \
       -vcodec libx264 -vpre baseline \
       -acodec libmp3lame -ab 48k \
       -f flv "$STREAM_URL"
       # can use flv as well
       # -vcodec flv \
       #-b 1000 -vframes 500 \
       # -threads 0 \
       #-f alsa -ac 2 -ar 22050 -i pulse \
       # -acodec pcm_s16le \
       #-acodec libmp3lame -ab 32k \
       #-acodec libmp3lame -ab 64k \
       #-acodec aac -strict experimental -ab 64k \
       #-acodec aac -strict experimental -ab 32k \
       #-acodec aac -strict experimental \

3   Notes

I dont like broadcasting on Ustream, because it requires you to click on Start Broadcasting on the website and you need to keep the Flash on. Which we Linux users all know, Flash has not-so-good performance. If you know how to bypass it, please leave a comment. However, it has nice beginner controls of your stream, I think you can put on overlay text by using it.

And be careful for your stream key, ffmpeg might print out that URL, which would include your key. I think your can lower your log level, but I dont want to do that, read ffmpeg(1) if you need to.


[1]http://www.justin.tv/settings/channel/info/ is gone, because Justin.tv is gone.

https://i.ytimg.com/vi/_X0pi8ObpMg/maxresdefault.jpg

This is just a quick announcement about what I am currently doing. I started a new project last week, x11grabr, X11 video GRABbeR. Its a fork of x11grab of libav/ffmpeg.

It can do what x11grab does and a little more as you can see in the screencast about. It has issues, but they wont be fixed anytime soon.

I list some tasks, go check up and give me some feedback and thoughts.

Its not ready for production use yet.

But it's not originally made from FFmpeg but libav. One day later after pushed to libav, my two patches got merged into FFmpeg.

Never heard of libav? Neither did I before until I read this thread FFmpeg-devel, then I decided to submit on libav-devel.

I got immediate response and many pointers about my code. I did wrote silly code, you can read my threads followmouse-1, followmouse-2, and showregion.

Basically, you can use
-f x11grab [-follow_mouse centered|PIXELS] [-show_region 1] ...
to enable these two options. You can see a video about them, but I changed they style, it's now dashed line box, not the solid lines as you see in that video.

Go grab FFmpeg or libav, if you can't wait.

(I did get a reply on FFmpeg-devel after the merge)

https://i.ytimg.com/vi/zOYb6iyXqs8/maxresdefault.jpg

I have wanted x11grab to be able to do things like recordMyDesktop and it does even better, because the mouse following in recordMyDesktop somewhat annoying when you watch the video you just make. The grabbing region moves as your mouse moves, which can give viewers headaches.

I did try to submit a patch to FFmpeg about a week ago when I first finished with little success, but I didnt get any response. So I decided to just fork it and do whatever I like. I can maintain my own copy since x11grab.c is pretty independent from other parts of FFmpeg.

Its so strange, when I searched for this mouse feature but I never read someone talked about. It looks as if everyone accepts that FFmpegs x11grab should have nothing to do with your mouse. I just dont get it, why didnt anyone wants that?

The funny unrelated thing I found during searching is some people even try to use xwininfo to get the window region, so they can supply to FFmpeg to grab that region. Thats so bad because most likely the video size isnt going to be a standard video size. It 99.999% would be something like 987x836, even you does a conversion to standard video size, its still bad because you does another encoding and resize/rescale with ratio remained your video.

Screencast is better not to be resized, in my opinion, pixel-to-pixel is the best.

Its sad that recordMyDesktop (rMD) seems like a dead project, no more commits. I see some people submitted patches and they would not be applied in anyway. I had thought about to send a email to the admin, but didnt do it.

With my own patch to x11grab, FFmpeg does better. I can output any kind of format I want. With rMD, you have only one format, which sometimes I have problem with.

Aside from this, I want to mention about forking on GitHub. I begin to like it, I forked a few projects and did my own changes to those projects. I didnt try to send a pull requests because I dont care and I dont want to wait for a reaction. I just maintain my own fork.

On GitHub, the Network tab is very useful, I can see what commits I dont have. I wish other hosting services [whispering Google] can have same Network tab. I dont particularly like Git, but I learned a few things since I started to fork and there is a plenty a lot about Git to learn. I never learned how to use CVS, but SVN is so easy to learn; same goes to Git vs. Hg, Git so complicated, so many commands and options, but now its changed.