Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created December 11, 2011 23:42
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edvakf/1463546 to your computer and use it in GitHub Desktop.
Save edvakf/1463546 to your computer and use it in GitHub Desktop.
#include "librtmp/rtmp.h"
#include "stdio.h"
#define BUFSIZE 4096
int main(int argc, char **argv) {
char buf[BUFSIZE];
FILE *fp;
RTMP *rtmp;
// initialize rtmp object
rtmp = RTMP_Alloc();
if (rtmp == NULL) {
printf("failed RTMP_Alloc\n");
return 1;
}
RTMP_Init(rtmp);
RTMP_SetupURL(rtmp, "rtmp://nleca04.live.nicovideo.jp:1935/liveedge/ts_00 swfUrl=http://live.nicovideo.jp/liveplayer.swf?20100531 conn=S:14101621:lv73915381:0:1323650469:cafdd4a4e0a50046 flashVer=MAC 10,0,32,18");
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
fp = fopen("tmp.flv", "w+");
if (fp == NULL) {
printf("failed to open file\n");
return 1;
}
// read stream and save to file
int total;
while (1) {
total = RTMP_Read(rtmp, buf, BUFSIZE);
if (total < 0) break; // success
if (fwrite(buf, total, 1, fp) != total) {
printf("failed to write to file\n");
break;
}
}
// finalize
fclose(fp);
free(buf);
RTMP_Close(rtmp);
RTMP_Free(rtmp);
return 1;
}
@edvakf
Copy link
Author

edvakf commented Dec 13, 2011

コンパイル手順
MacPortsでrtmpdumpをインストールしてたら、

mkdir rtmpsave
cd rtmpsave
cp /opt/local/var/macports/distfiles/rtmpdump/rtmpdump-2.3.tgz .
tar xzf rtmpdump-2.3.tgz
cp -r rtmpdump-2.3/librtmp .
cd librtmp
make CRYPTO= SHARED=
cd ..

これでまずlibrtmpをコンパイルし、librtmp.aを作る。次に、

curl https://raw.github.com/gist/1463546/6e8cceaf0cfb2e1cbfb76b56e792691727b72df4/rtmpsave.c -o rtmpsave.c
gcc -c rtmpsave.c
gcc rtmpsave.o -L./librtmp -lrtmp -o rtmpsave

これでrtmpsave(実行ファイル)を作る。

@edvakf
Copy link
Author

edvakf commented Dec 13, 2011

使い方
まずニコニコ動画にログインしてセッションIDを取得

curl -k -D /dev/stdout -d mail=ほにゃらら -d password=ほにゃらら https://secure.nicovideo.jp/secure/login

次に、セッションIDをcookieとしてXMLを取得

curl -b user_session=user_session_ほにゃらら 'http://watch.live.nicovideo.jp/api/getplayerstatus?v=lvほにゃらら' -o tmp.xml

XMLから動画のURLとチケットを取得。

cat tmp.xml | sed 's/.*<quesheet>\(.*\)<\/quesheet>.*/\1/'
cat tmp.xml | sed 's/.*<ticket>\(.*\)<\/ticket>.*/\1/'

URLは例えば

rtmp://nlpoca02.live.nicovideo.jp:1935/fileorigin/ts_00,/content/20111211/lv73915381_094151546000_2_aa79cc.f4v?1323769877:30:52ae97da8354004f

みたいな感じなので、これの",/content"というところを"/mp4:content"に変える。最後にrtmpsaveを使う。

./rtmpsave "ユーアールエル" "チケット"

みたいな感じ。

@edvakf
Copy link
Author

edvakf commented Dec 13, 2011

↑をするためのRubyスクリプト

#!/usr/bin/env ruby

email = ""
password = ""

if ARGV.length < 1
  puts "usage: ruby rtmvsave.rb lv******** [cookie]"
  exit 1
end

cookie = nil

if ARGV.length == 2
  cookie = ARGV[1]
else
  cookie = nil
  header = `curl -k -D /dev/stdout -d mail=#{email} -d password=#{password} https://secure.nicovideo.jp/secure/login`
  if header =~ /(user_session=user_session_.*?);/
    cookie = $1
  end
end

exit 1 if cookie == nil

xml = `curl -b #{cookie} 'http://watch.live.nicovideo.jp/api/getplayerstatus?v=#{ARGV[0]}'`

url = nil
ticket = nil

if xml =~ /<que.*?(rtmp:\/\/.*?)<\/que>/
  url = $1.sub(",/content", "/mp4:content")
end

if xml =~ /<ticket>(.*?)<\/ticket>/
  ticket = $1
end

exit 1 if url == nil or ticket == nil

command = "./rtmpsave '#{url}' '#{ticket}'"
puts "running command:\n  #{command}"
system(command)

使い方は、メールとパスワードを入力して

ruby rtmvsave.rb lv********

と実行。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment