Created
December 11, 2011 23:42
-
-
Save edvakf/1463546 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "librtmp/rtmp.h" | |
int main(int argc, char *argv[]) { | |
if (argc < 3) { | |
printf("usage : %s url ticket\n", argv[0]); | |
return 1; | |
} | |
// initialize rtmp object | |
RTMP *rtmp = RTMP_Alloc(); | |
if (rtmp == NULL) { | |
printf("failed RTMP_Alloc\n"); | |
return 1; | |
} | |
RTMP_Init(rtmp); | |
// build url (url must be non-constant string, so dynamically allocate memory) | |
char url[1024]; | |
char *swfUrl = "http://live.nicovideo.jp/liveplayer.swf?20100531"; | |
sprintf(url, "%s conn=S:%s swfUrl=%s", argv[1], argv[2], swfUrl); | |
printf("%s\n", url); | |
RTMP_SetupURL(rtmp, url); | |
if (!RTMP_Connect(rtmp, NULL)) { | |
printf("failed to establish RTMP connection\n"); | |
return 1; | |
} | |
if (!RTMP_ConnectStream(rtmp, 0)) { | |
printf("failed to establish RTMP session\n"); | |
return 1; | |
} | |
// open file | |
FILE *fp = fopen("tmp.flv", "w+"); | |
if (fp == NULL) { | |
printf("failed to open file\n"); | |
return 1; | |
} | |
// read stream and save to file | |
long nRead; | |
long size = 0; | |
int bufsize = 64 * 1024; | |
char *buf = (char *)malloc(bufsize); | |
do { | |
nRead = RTMP_Read(rtmp, buf, bufsize); | |
if (nRead < 0) break; // success | |
/*if (nRead == 0) {*/ | |
/*printf("chunk size is zero; fail?\n");*/ | |
/*break;*/ | |
/*}*/ | |
if (nRead != fwrite(buf, sizeof(char), nRead, fp)) { | |
printf("failed to write to file\n"); | |
return 1; | |
} | |
size += nRead; | |
if (nRead > 0) | |
printf("total bytes read=%ld, chunk size=%ld, time=%.2fsec\n", size, nRead, (double)rtmp->m_read.timestamp/1000.0); | |
} while(!RTMP_ctrlC && nRead > -1 && RTMP_IsConnected(rtmp)); | |
// finalize | |
free(buf); | |
fclose(fp); | |
RTMP_Close(rtmp); | |
RTMP_Free(rtmp); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
↑をするためのRubyスクリプト
使い方は、メールとパスワードを入力して
と実行。