├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── demo_link.png └── src ├── args.c ├── args.h ├── encode.c ├── encode.h ├── frame.c ├── frame.h ├── main.c └── tl.1 /.gitignore: -------------------------------------------------------------------------------- 1 | /tl 2 | *.o 3 | .*.sw[op] 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Ryan Jacobs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | ``` 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ## Options 2 | 3 | # Name of executable 4 | EXE?=tl 5 | 6 | # Compiler statically? (default is dynamic libs) 7 | STATIC?=no 8 | 9 | # Installation prefix 10 | PREFIX?=/usr 11 | 12 | # Compiler Options 13 | CC?=gcc 14 | STRIP?=strip 15 | CFLAGS=-c -O2 -Wall -std=c99 -pedantic 16 | DEFINES=-D_DEFAULT_SOURCE 17 | 18 | MAN=$(EXE).1 19 | SOURCES=$(shell find src/ -type f -name '*.c') 20 | OBJECTS=$(SOURCES:.c=.o) 21 | 22 | ifeq ($(STATIC),yes) 23 | DEFINES+=-D_TL_STATIC_BUILD="\"yes\"" 24 | 25 | # Using a custom build of ffmpeg with: 26 | # 27 | # > git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg 28 | # > git checkout release/2.7 29 | # > ./configure --prefix=/home/vagrant/builds/usr --enable-libx264 --enable-gpl 30 | # --disable-libopus --disable-vaapi --enable-static --disable-shared 31 | # > make && make install 32 | # 33 | CFLAGS+=-I/home/vagrant/builds/usr/include 34 | LDFLAGS=-L/home/vagrant/builds/usr/lib\ 35 | -static -lX11 -lxcb -lXau -lXdmcp\ 36 | /home/vagrant/builds/usr/lib/libavformat.a\ 37 | /home/vagrant/builds/usr/lib/libavcodec.a\ 38 | /home/vagrant/builds/usr/lib/libavfilter.a\ 39 | /home/vagrant/builds/usr/lib/libavutil.a\ 40 | /home/vagrant/builds/usr/lib/libswresample.a\ 41 | /home/vagrant/builds/usr/lib/libswscale.a\ 42 | -lx264 -lm -lz -ldl -lpthread -llzma\ 43 | -static-libgcc 44 | 45 | # If only system libraries worked... 46 | #LDFLAGS=-static -lX11 -lxcb -lXau -lXdmcp\ 47 | -lx264 -lm -lz -ldl -lpthread -llzma\ 48 | -lavformat -lavcodec -lavfilter -lavutil -lswresample -lswscale\ 49 | -static-libgcc 50 | else 51 | DEFINES+=-D_TL_STATIC_BUILD="\"no\"" 52 | 53 | LDFLAGS=-lX11 -lavformat -lavcodec -lavutil -lswscale -lm 54 | endif 55 | 56 | DEFINES+=\ 57 | -D_TL_COMPILE_DATE="\"$(shell date '+%b %d, %Y - %T %Z')\""\ 58 | -D_TL_COMMIT_SHA="\"$(shell git rev-parse --verify HEAD)\""\ 59 | -D_TL_CFLAGS="\"$(CFLAGS)\""\ 60 | -D_TL_LDFLAGS="\"$(LDFLAGS)\"" 61 | 62 | all: $(SOURCES) $(EXE) 63 | 64 | $(EXE): $(OBJECTS) 65 | $(CC) $(OBJECTS) $(LDFLAGS) -o $@ 66 | $(STRIP) $(EXE) 67 | 68 | .c.o: 69 | @echo $(CC) $(CFLAGS) $< -o $@ 70 | @$(CC) $(CFLAGS) $(DEFINES) $< -o $@ 71 | 72 | install: all 73 | install -Dm 775 $(EXE) $(PREFIX)/bin/$(EXE) 74 | install -Dm 644 src/tl.1 $(PREFIX)/share/man/man1/$(MAN) 75 | 76 | uninstall: 77 | @rm -vf $(PREFIX)/bin/$(EXE) 78 | @rm -vf $(PREFIX)/share/man/man1/$(MAN) 79 | 80 | clean: 81 | @rm -vf $(EXE) 82 | @find src/ -type f -name '*.o' -exec rm -vf {} \; 83 | 84 | .PHONY: clean 85 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tl 2 | #### Painless timelapsing to show off your productivity. 3 | 4 | **tl** progressively captures and encodes screenshots to an MP4 file. Unlike 5 | other timelapse tools, it doesn't store thousands and thousands of images only 6 | to encode them later. **It captures and encodes in realtime!** It is *not* going 7 | to thrash your disk with gigs upon gigs of screenshots, and it is *not* going 8 | to record at 30 fps when you only really need 1 fps. 9 | 10 | It's a great way to show off how productive you are! Plus it looks cool! So 11 | hit record and start up a coding session! Or boot up Photoshop (or more likely 12 | Gimp) and start drawing! 13 | 14 | [](https://vimeo.com/133315382) 15 | 16 | (It can also be used as a decent screen recorder with: `tl -d .0333 -r 30`.) 17 | 18 | ## Downloading 19 | #### Pre-compiled, statically linked binaries can be downloaded in the [releases](https://github.com/ryanmjacobs/tl/releases). 20 | 21 | ##### For example you can download and run v0.02: 22 | ```bash 23 | $ wget https://github.com/ryanmjacobs/tl/releases/download/v0.02/tl-v0.02 24 | $ ./tl-v0.02 --version 25 | ``` 26 | 27 | ## Compiling from scratch 28 | #### Download the required libraries: 29 | 30 | ##### Arch Linux: 31 | ```bash 32 | $ pacman -S --needed base-devel libx11 ffmpeg 33 | ``` 34 | ##### Ubuntu (14.04) 35 | ```bash 36 | $ apt-get install build-essential libx11-dev libswscale-dev libavcodec-dev libavformat-dev 37 | ``` 38 | 39 | (If you know the correct packages for your distro, please submit a pull 40 | request and I'll add it.) 41 | 42 | #### Then just run: 43 | ```bash 44 | $ make 45 | $ ./tl --help 46 | $ ./tl 47 | ``` 48 | 49 | #### To install: 50 | ```bash 51 | $ make install 52 | # or... 53 | $ make install PREFIX=/usr/local 54 | ``` 55 | 56 | # Contributing 57 | Feel free to submit any ideas, questions, or problems by reporting an issue. 58 | Or, if you're feeling bit brave, submit a pull request. :grimacing: 59 | 60 | ## Packages 61 | * AUR: https://aur4.archlinux.org/packages/tl/ 62 | 63 | ## Todo 64 | * Figure out how to encode an h.264 stream directly into an MP4 container. 65 | * Recompress the video at the end. (Basically `ffmpeg -i in.mp4 -c:v libx264 out.mp4` but using the API.) 66 | * Maybe add a GUI? 67 | * Somehow support Mac OS X *and* Windows... 68 | * Support drawing a capture box (or even just defining a capture box.) 69 | * Fix the `Using 'XXX' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking` warning. 70 | 71 | ## Notes 72 | * After recording, running `ffmpeg -i timelapse.mp4 -c:v libx264 out.mp4` will 73 | compress the video significantly (around 75%). 74 | * There is a C++ branch that you can checkout. I prefer to stick with C, but we 75 | may need to move to C++ in order to support other OSes or GUIs. The C++ branch 76 | compiles fine, but as of now, is a little out of date. 77 | 78 | ## License 79 | MIT License - see the [LICENSE](https://raw.githubusercontent.com/ryanmjacobs/tl/master/LICENSE) 80 | 81 | Copyright (c) 2018 Ryan Jacobs 82 | -------------------------------------------------------------------------------- /demo_link.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ryanmjacobs/tl/516b36f791d34c5c4c47df9230701a78792222d0/demo_link.png -------------------------------------------------------------------------------- /src/args.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file args.c 3 | * @brief Parse and process command line arguments. 4 | * @author Ryan Jacobs 5 | * @date March 19, 2015 6 | * @bug No known bugs. 7 | */ 8 | 9 | #include "args.h" 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | enum { 17 | OPT_VERSION 18 | }; 19 | 20 | static const char *version_msg = 21 | "tl v0.03-alpha\n\n" 22 | 23 | "Compiled : "_TL_COMPILE_DATE"\n" 24 | " Commit : "_TL_COMMIT_SHA"\n" 25 | " Static : "_TL_STATIC_BUILD"\n" 26 | 27 | " CFLAGS : "_TL_CFLAGS"\n" 28 | " LDFLAGS : "_TL_LDFLAGS"\n" 29 | 30 | "\n" 31 | "Copyright (C) 2015 Ryan Jacobs\n"; 32 | 33 | static const char *help_msg = 34 | "Usage: %s [-h] [options...]\n" 35 | "Record and render timelapses.\n\n" 36 | " -f, --frames Number of frames to record. 0 means infinite\n" 37 | " -d, --delay Delay in seconds between each screenshot\n" 38 | " -r, --framerate Set playback fps for the encoded video\n" 39 | " -o, --output Output filename\n" 40 | " -D, --display X display name, default is :0\n" 41 | " -h, --help Display this help and exit\n" 42 | " --version Display version information and exit\n\n" 43 | "Report bugs to \n"; 44 | 45 | static const struct option long_options[] = { 46 | { "frames", optional_argument, NULL, 'f' }, 47 | { "delay", optional_argument, NULL, 'd' }, 48 | { "rate", optional_argument, NULL, 'r' }, 49 | { "output", optional_argument, NULL, 'o' }, 50 | { "display", optional_argument, NULL, 'D' }, 51 | { "help", optional_argument, NULL, 'h' }, 52 | { "version", no_argument, NULL, OPT_VERSION }, 53 | { NULL, 0, NULL, 0 } 54 | }; 55 | 56 | static int file_exists(const char *fname) { 57 | FILE *fp = fopen(fname, "r"); 58 | 59 | if (fp) { 60 | fclose(fp); 61 | return 1; 62 | } else { 63 | return 0; 64 | } 65 | } 66 | 67 | struct args_t parse_args(int argc, char **argv) { 68 | struct args_t args = { 69 | .frames = 0, // aka infinite 70 | .delay = 1000000, // 1s 71 | .framerate = 15, 72 | .fname = "timelapse.mp4", 73 | .x_display_name = ":0" 74 | }; 75 | 76 | /* decide on the default filename */ 77 | if (file_exists(args.fname)) { 78 | unsigned int i; 79 | 80 | for (i = 1; i < UINT_MAX; i++) { 81 | int len = snprintf(NULL, 0, "timelapse_%u.mp4", i); 82 | args.fname = malloc(++len); 83 | snprintf(args.fname, len, "timelapse_%u.mp4", i); 84 | 85 | if (!file_exists(args.fname)) 86 | break; 87 | 88 | free(args.fname); 89 | } 90 | } 91 | 92 | char c; 93 | while ((c = getopt_long(argc, argv, "f:d:r:o:D:h", long_options, NULL)) != -1) { 94 | switch (c) { 95 | case 'f': 96 | if ((args.frames = atoi(optarg)) < 0) { 97 | fprintf(stderr, "error: '%s' is not a valid number of frames\n", optarg); 98 | exit(1); 99 | } 100 | break; 101 | case 'd': 102 | if ((args.delay = (unsigned int) (atof(optarg)*1000000.0)) < 0) { 103 | fprintf(stderr, "error: '%s' is not a valid delay interval\n", optarg); 104 | exit(1); 105 | } 106 | break; 107 | case 'r': 108 | if ((args.framerate = atoi(optarg)) <= 0) { 109 | fprintf(stderr, "error: '%s' is not a valid framerate\n", optarg); 110 | exit(1); 111 | } 112 | break; 113 | case 'o': 114 | args.fname = optarg; 115 | break; 116 | case 'h': 117 | fprintf(stdout, help_msg, argv[0]); 118 | exit(0); 119 | break; 120 | case 'D': 121 | args.x_display_name = optarg; 122 | break; 123 | case OPT_VERSION: 124 | fputs(version_msg, stdout); 125 | exit(0); 126 | break; 127 | default: 128 | fprintf(stderr, "Try `%s --help` for more information.\n", argv[0]); 129 | exit(1); 130 | break; 131 | } 132 | } 133 | 134 | return args; 135 | } 136 | -------------------------------------------------------------------------------- /src/args.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file args.h 3 | * @brief Parse and process command line arguments. 4 | * @author Ryan Jacobs 5 | * @date March 19, 2015 6 | * @bug No known bugs. 7 | */ 8 | 9 | #ifndef ARGS_H 10 | #define ARGS_H 11 | 12 | struct args_t { 13 | int frames; /* number of frames to record. 0 means infinite */ 14 | int delay; /* delay between each screenshot (in seconds) */ 15 | int framerate; /* encoded playback framerate */ 16 | char *fname; /* name of output file */ 17 | char *x_display_name; /* x display name, e.g. :0 */ 18 | }; 19 | 20 | struct args_t parse_args(int argc, char **argv); 21 | 22 | #endif /* ARGS_H */ 23 | -------------------------------------------------------------------------------- /src/encode.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file encode.c 3 | * @brief Encode raw frames to video. 4 | * @author Ryan Jacobs 5 | * @date March 18, 2015 6 | * @bug No known bugs. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "frame.h" 24 | 25 | extern int CAUGHT_SIGINT; 26 | 27 | #define RNDTO2(X) ( ( (X) & 0xFFFFFFFE ) 28 | #define RNDTO32(X) ( ( (X) % 32 ) ? ( ( (X) + 32 ) & 0xFFFFFFE0 ) : (X) ) 29 | 30 | // http://stackoverflow.com/questions/24057248/ffmpeg-undefined-references-to-av-frame-alloc 31 | #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1) 32 | #define av_frame_alloc avcodec_alloc_frame 33 | #define av_frame_free avcodec_free_frame 34 | #endif 35 | 36 | void encode_loop(const char *filename, long long int frames, unsigned int delay, 37 | int framerate) 38 | { 39 | FILE *f; 40 | 41 | /* abort if file already exists */ 42 | if ((f = fopen(filename, "r")) != NULL) { 43 | fclose(f); 44 | fprintf(stderr, "error: file '%s' already exists\n", filename); 45 | exit(1); 46 | } 47 | 48 | AVCodec *codec; 49 | AVCodecContext *c= NULL; 50 | int ret, got_output; 51 | unsigned int i = 0; 52 | AVFrame *frame; 53 | AVPacket pkt; 54 | uint8_t endcode[] = { 0, 0, 1, 0xb7 }; 55 | 56 | /* find the mpeg1 video encoder */ 57 | codec = avcodec_find_encoder(AV_CODEC_ID_H264); 58 | if (!codec) { 59 | fprintf(stderr, "Codec not found\n"); 60 | exit(1); 61 | } 62 | c = avcodec_alloc_context3(codec); 63 | if (!c) { 64 | fprintf(stderr, "Could not allocate video codec context\n"); 65 | exit(1); 66 | } 67 | /* put sample parameters */ 68 | //c->bit_rate = 400000; 69 | /* resolution must be a multiple of two */ 70 | c->width = get_frame_width(); 71 | c->height = get_frame_height(); 72 | /* frames per second */ 73 | c->time_base = (AVRational){1,framerate}; 74 | c->gop_size = 10; /* emit one intra frame every ten frames */ 75 | c->max_b_frames = 1; 76 | c->pix_fmt = AV_PIX_FMT_YUV420P; 77 | 78 | av_opt_set(c->priv_data, "preset", "slow", 0); 79 | av_opt_set_int(c, "crf", 20, AV_OPT_SEARCH_CHILDREN); 80 | 81 | /* open codec */ 82 | if (avcodec_open2(c, codec, NULL) < 0) { 83 | fprintf(stderr, "Could not open codec\n"); 84 | exit(1); 85 | } 86 | 87 | /* open file */ 88 | f = fopen(filename, "wb"); 89 | if (!f) { 90 | fprintf(stderr, "Could not open %s\n", filename); 91 | exit(1); 92 | } 93 | 94 | /* allocate video frame */ 95 | frame = av_frame_alloc(); 96 | if (!frame) { 97 | fprintf(stderr, "Could not allocate video frame\n"); 98 | exit(1); 99 | } 100 | frame->format = c->pix_fmt; 101 | frame->width = c->width; 102 | frame->height = c->height; 103 | /* the image can be allocated by any means and av_image_alloc() is 104 | * just the most convenient way if av_malloc() is to be used */ 105 | ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height, 106 | c->pix_fmt, 32); 107 | if (ret < 0) { 108 | fprintf(stderr, "Could not allocate raw picture buffer\n"); 109 | exit(1); 110 | } 111 | 112 | /* will we run forever? */ 113 | int inf = !frames; 114 | 115 | while (1) { 116 | if (CAUGHT_SIGINT) 117 | break; 118 | 119 | if (!inf && --frames < 0) 120 | break; 121 | 122 | usleep(delay); 123 | 124 | av_init_packet(&pkt); 125 | pkt.data = NULL; // packet data will be allocated by the encoder 126 | pkt.size = 0; 127 | fflush(stdout); 128 | 129 | struct SwsContext *ctx = 130 | sws_getContext(c->width, c->height, AV_PIX_FMT_RGB24, 131 | c->width, c->height, AV_PIX_FMT_YUV420P, 132 | 0, 0, 0, 0); 133 | 134 | unsigned char *rgb_buf = grab_frame(); 135 | const uint8_t *data_in[1] = { rgb_buf }; 136 | int inline_size[1] = { 3*c->width }; 137 | sws_scale(ctx, data_in, inline_size, 0, c->height, 138 | frame->data, frame->linesize); 139 | 140 | frame->pts = i; 141 | /* encode the image */ 142 | ret = avcodec_encode_video2(c, &pkt, frame, &got_output); 143 | if (ret < 0) { 144 | fprintf(stderr, "Error encoding frame\n"); 145 | exit(1); 146 | } 147 | if (got_output) { 148 | printf("Wrote frame %d (size=%d)\n", i++, pkt.size); 149 | fwrite(pkt.data, 1, pkt.size, f); 150 | av_free_packet(&pkt); 151 | } 152 | 153 | free(rgb_buf); 154 | sws_freeContext(ctx); 155 | } 156 | /* get the delayed frames */ 157 | for (got_output = 1; got_output; i++) { 158 | fflush(stdout); 159 | ret = avcodec_encode_video2(c, &pkt, NULL, &got_output); 160 | if (ret < 0) { 161 | fprintf(stderr, "Error encoding frame\n"); 162 | exit(1); 163 | } 164 | if (got_output) { 165 | printf("Wrote frame %3d (size=%5d)\n", i, pkt.size); 166 | fwrite(pkt.data, 1, pkt.size, f); 167 | av_free_packet(&pkt); 168 | } 169 | } 170 | /* add sequence end code to have a real mpeg file */ 171 | fwrite(endcode, 1, sizeof(endcode), f); 172 | fclose(f); 173 | avcodec_close(c); 174 | av_free(c); 175 | av_freep(&frame->data[0]); 176 | av_frame_free(&frame); 177 | printf("\n"); 178 | } 179 | -------------------------------------------------------------------------------- /src/encode.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file encode.h 3 | * @brief Encode raw frames to video. 4 | * @author Ryan Jacobs 5 | * @date March 18, 2015 6 | * @bug No known bugs. 7 | */ 8 | 9 | #ifndef ENCODE_H 10 | #define ENCODE_H 11 | 12 | /** 13 | * Main encode loop. 14 | * Captures RGB frames and writes them to an h264 stream. 15 | * 16 | * @param filename Output filename. 17 | * @param frames Number of frames to record. Use 0 to record forever. 18 | * @param delay Delay in seconds between each screenshot. 19 | * @param framerate FPS for video output. 20 | */ 21 | void encode_loop(const char *filename, long long int frames, unsigned int delay, 22 | int framerate); 23 | 24 | #endif /* ENCODE_H */ 25 | -------------------------------------------------------------------------------- /src/frame.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file frame.c 3 | * @brief Generate each frame. 4 | * @author Ryan Jacobs 5 | * @date March 18, 2015 6 | * @bug No known bugs. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | #include "frame.h" 20 | 21 | struct pixel_t { 22 | unsigned char red; 23 | unsigned char green; 24 | unsigned char blue; 25 | }; 26 | 27 | typedef uint16_t WORD; 28 | 29 | static Screen *scr; 30 | static Display *dpl; 31 | static struct pixel_t grab_pixel(XImage *img, int x, int y); 32 | 33 | int init_X(const char *display_name) { 34 | dpl = XOpenDisplay(display_name); 35 | if (dpl == NULL) { 36 | fprintf(stderr, "error: could not open X display %s\n", display_name); 37 | exit(1); 38 | } 39 | 40 | scr = XDefaultScreenOfDisplay(dpl); 41 | if (dpl == NULL) { 42 | fputs("error: could not access the default screen of the X display\n", stderr); 43 | exit(1); 44 | } 45 | 46 | return 0; 47 | } 48 | 49 | int free_X(void) { 50 | XCloseDisplay(dpl); 51 | 52 | return 0; 53 | } 54 | 55 | int get_frame_width(void) { 56 | return XWidthOfScreen(scr); 57 | } 58 | 59 | int get_frame_height(void) { 60 | return XHeightOfScreen(scr); 61 | } 62 | 63 | unsigned char *grab_frame(void) { 64 | int width = XWidthOfScreen(scr); 65 | int height = XHeightOfScreen(scr); 66 | 67 | XImage *img = XGetImage(dpl, RootWindow(dpl, DefaultScreen(dpl)), 0, 0, 68 | width, height, AllPlanes, ZPixmap); 69 | 70 | unsigned char *rgb = malloc(3*width*height); 71 | 72 | for (int y = 0; y < height; y++) { 73 | for (int x = 0; x < width; x++) { 74 | struct pixel_t p = grab_pixel(img, x, y); 75 | 76 | rgb[3*(x + width*y) + 0] = p.red; 77 | rgb[3*(x + width*y) + 1] = p.green; 78 | rgb[3*(x + width*y) + 2] = p.blue; 79 | } 80 | } 81 | 82 | XDestroyImage(img); 83 | return rgb; 84 | } 85 | 86 | // http://www.opensource.apple.com/source/tcl/tcl-95/tk/tk/win/tkWinImage.c 87 | static struct pixel_t grab_pixel(XImage *img, int x, int y) { 88 | struct pixel_t p = {0}; 89 | unsigned char *srcPtr = (unsigned char *) &(img->data[(y * img->bytes_per_line) 90 | + ((x * img->bits_per_pixel) / NBBY)]); 91 | 92 | switch (img->bits_per_pixel) { 93 | case 32: 94 | case 24: 95 | p.red = srcPtr[2]; 96 | p.green = srcPtr[1]; 97 | p.blue = srcPtr[0]; 98 | break; 99 | case 16: 100 | p.red = ((((WORD*)srcPtr)[0]) >> 7) & 0xf8; 101 | p.green = ((((WORD*)srcPtr)[0]) >> 2) & 0xf8; 102 | p.blue = ((((WORD*)srcPtr)[0]) << 3) & 0xf8; 103 | break; 104 | //case 8: 105 | // pixel = srcPtr[0]; 106 | // break; 107 | //case 4: 108 | // pixel = ((x%2) ? (*srcPtr) : ((*srcPtr) >> 4)) & 0x0f; 109 | // break; 110 | //case 1: 111 | // pixel = ((*srcPtr) & (0x80 >> (x%8))) ? 1 : 0; 112 | // break; 113 | default: 114 | fprintf(stderr, "error: %d-bit pixel displays are not yet " 115 | "supported. Go file a bug.\n", img->bits_per_pixel); 116 | exit(1); 117 | break; 118 | } 119 | 120 | return p; 121 | } 122 | -------------------------------------------------------------------------------- /src/frame.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file frame.h 3 | * @brief Generate each frame. 4 | * @author Ryan Jacobs 5 | * @date March 18, 2015 6 | * @bug No known bugs. 7 | */ 8 | 9 | #ifndef FRAME_H 10 | #define FRAME_H 11 | 12 | int init_X(const char *display_name); 13 | int free_X(void); 14 | unsigned char *grab_frame(void); 15 | int get_frame_width(void); 16 | int get_frame_height(void); 17 | 18 | #endif /* FRAME_H */ 19 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file main.c 3 | * @author Ryan Jacobs 4 | * @date March 18, 2015 5 | * @bug No known bugs. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | 16 | #include "args.h" 17 | #include "frame.h" 18 | #include "encode.h" 19 | 20 | int CAUGHT_SIGINT = 0; 21 | 22 | static void sigint_handler(int signal) { 23 | CAUGHT_SIGINT = 1; 24 | } 25 | 26 | static int mp4_wrapper(const char *ifname); 27 | 28 | int main(int argc, char **argv) { 29 | struct args_t args = parse_args(argc, argv); 30 | 31 | signal(SIGINT, sigint_handler); 32 | 33 | init_X(args.x_display_name); 34 | avcodec_register_all(); 35 | encode_loop(args.fname, args.frames, args.delay, args.framerate); 36 | free_X(); 37 | 38 | /** 39 | * This is just a quick hack to get an MP4 container by using 40 | * ffmpeg externally. If anyone knows how we can get an MP4 41 | * container without doing that, *please* submit a pull request 42 | * or contact me! 43 | */ 44 | int ret = mp4_wrapper(args.fname); 45 | if (ret != 0) { 46 | puts("We were not able to dump to an MP4 container :("); 47 | puts("You can still play it, but some video players will choke on it.\n"); 48 | 49 | if (ret == -1) { 50 | puts("Next time install ffmpeg/avconv first: e.g. `sudo apt-get install ffmpeg`"); 51 | puts("(It's optional, but will shrink the file size and play more smoothly."); 52 | } 53 | } 54 | 55 | return 0; 56 | } 57 | 58 | /** 59 | * Quick hack... see main() for more info. 60 | */ 61 | static int mp4_wrapper(const char *ifname) { 62 | int len; 63 | const char *encoder; 64 | 65 | /* use ffmpeg/avconv (whichever is available) */ 66 | if (!system("which ffmpeg &>/dev/null")) { 67 | encoder = "ffmpeg"; 68 | } else if (!system("which avconv &>/dev/null")) { 69 | encoder = "avconv"; 70 | } else { 71 | return -1; 72 | } 73 | 74 | /* output filename = input filename + ".mp4" */ 75 | len = strlen(ifname); 76 | char *ofname = strndup(ifname, len +1 +4); 77 | if (ofname == NULL) { 78 | perror("strndup"); 79 | return -2; 80 | } 81 | strcat(ofname, ".mp4"); 82 | 83 | /* create command string */ 84 | const char *fmt = "%s -y -i %s -c:v copy -an -loglevel quiet %s"; 85 | len = snprintf(NULL, 0, fmt, encoder, ifname, ofname); 86 | char *cmd = malloc(++len); 87 | if (cmd == NULL) { 88 | perror("malloc"); 89 | return -2; 90 | } 91 | snprintf(cmd, len, fmt, encoder, ifname, ofname); 92 | 93 | /* run command and if successful delete input file */ 94 | if (system(cmd) == 0) { 95 | unlink(ifname); 96 | rename(ofname, ifname); 97 | 98 | printf("# Run `ffmpeg -i %s -c:v libx264 small.mp4` to shrink the " 99 | "filesize by about 75%%.\n", ifname); 100 | } 101 | 102 | free(cmd); 103 | free(ofname); 104 | 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /src/tl.1: -------------------------------------------------------------------------------- 1 | .\" 2 | .\" Copyright (C) 2015 Ryan Jacobs 3 | .\" 4 | 5 | .TH tl 1 "July 12, 2015" 6 | .SH NAME 7 | tl - Painless timelapsing to show off your productivity. 8 | .SH tl 9 | .B tl [-h] [options...] 10 | 11 | .SH DESCRIPTION 12 | .B tl 13 | progressively takes screenshots and writes them to an h.264 encoded file. The nice thing about 14 | .B tl 15 | is that instead of collecting thousands of screen caps that quickly add up to gigs of diskspace, it progressively encodes the images to a playable file. There isn't a long render time at the end because all of the images are encoded as they are captured. 16 | 17 | .PP 18 | .TP 19 | .B -f, --frames 20 | Number of frames to record. 0 means infinite and is the default. 21 | .TP 22 | .B -d, --delay 23 | Delay in seconds between each screenshot. Default is 1. 24 | .TP 25 | .B -r, --framerate 26 | Set playback fps for the encoded video. Default is 15. 27 | .TP 28 | .B -o, --output 29 | Output filename. Default is "timelapse.mp4". 30 | .TP 31 | .B -D, --display 32 | X display name. Default is ":0". 33 | .TP 34 | .B -h, --help 35 | Display the help and exit. 36 | .TP 37 | .B --version 38 | Display version information and exit 39 | .PP 40 | 41 | .SH BUGS 42 | Please file bugs, suggestions, and pull requests to 43 | 44 | .SH AUTHOR 45 | Ryan Jacobs 46 | --------------------------------------------------------------------------------