Skip to content

Instantly share code, notes, and snippets.

@keiya
Created February 22, 2016 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save keiya/cb9e2901ccc1e813c634 to your computer and use it in GitHub Desktop.
Save keiya/cb9e2901ccc1e813c634 to your computer and use it in GitHub Desktop.
Split Motion-JPEG (MJPEG) Stream into JPEG files; for Syma X5SW WiFi FPV Camera. ; also included realtime streaming shell script which is using VLC.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_FRAME_SIZE 256*1024 // KB
#define CONTENT_LENGTH "Content-Length: "
#define PAYLOAD "\r\n"
int main()
{
char buf[MAX_FRAME_SIZE];
//size_t rs = fread(buf, MAX_FRAME_SIZE, 1, stdin);
// parse content-length
int length;
int seq = 0;
while (1) {
while( fgets(buf, MAX_FRAME_SIZE, stdin) != NULL )
{
char *length_start = strstr(buf,CONTENT_LENGTH);
if (length_start == NULL) continue;
char *length_end = strstr(length_start,"\r\n");
int tmp = *length_end;
*length_end = 0;
length_start += strlen(CONTENT_LENGTH);
*length_end = tmp;
length = atoi(length_start);
break;
}
while( fgets(buf, MAX_FRAME_SIZE, stdin) != NULL )
{
// parse body
char *payload = strstr(buf,PAYLOAD);
if (payload != buf) continue; // if not start with "\r\n"
break;
}
if (fread(buf,length,1,stdin) == 0) break;
fwrite(buf,length,1,stdout);
char fname[16];
snprintf(fname,15,"%05d.jpg",seq++);
FILE *out = fopen(fname,"w");
fwrite(buf,length,1,out);
fclose(out);
}
}
PARSER="$HOME/hgfs/Dropbox/symafpv/a.out"
HOST="http://192.168.1.1:80"
DATE=`date +%F_%T`
mkdir -p $DATE
cd $DATE
ID=`wget -q -O - "$HOST/request_av.cgi?User=admin&pwd=" | grep "var id=" | grep -o -P "[0-9]+"`
wget -q -O - "$HOST/Videostream.cgi?User=admin&pwd=&id=$ID" | $PARSER | vlc /dev/stdin
avconv -framerate 15 -i %05d.jpg -codec copy encoded15.mov
rm *.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment