3
4

More than 5 years have passed since last update.

node-ffiメモ

Last updated at Posted at 2014-09-10

背景

npmでもnode-ffiが0.11系にも対応したので、ffmpegに付属のlibavformatを使って、動画の撮影日を調べてようとしている。

C言語の型の対応

  • char*はstring
  • ポインターはffiとは別のrefパッケージを使う
  • ポインターもJavaScript側では領域の確保的な操作が必要
  • NULLポインターはnullでOK

libavformatの例

とりあえず、関数を呼ぶだけのコード

var ffi = require('ffi');
var ref = require('ref');

var AVFormatContext = ref.types.void;
var AVFormatContextPtr = ref.refType(AVFormatContext);
var AVFormatContextPtrPtr = ref.refType(AVFormatContextPtr);
var stringPtr = ref.refType("string");
var AVInputFormat = ref.types.void;
var AVInputFormatPtr = ref.refType(AVInputFormat);
var AVDictionary = ref.types.void;
var AVDictionaryPtr = ref.refType(AVDictionary);
var AVDictionaryPtrPtr = ref.refType(AVDictionaryPtr);

// 共有ライブラリで使いたい関数のプロトタイプ宣言?を行う。
var libavformat = ffi.Library('libavformat', {
  "av_register_all":["void",[]],
  "avformat_open_input":["int",[AVFormatContextPtrPtr, "string", AVInputFormatPtr,AVDictionaryPtrPtr]]
});

var contextPtrPtr = ref.alloc(AVFormatContextPtrPtr);
var formatPtr = ref.alloc(AVInputFormatPtr);
var dictPtrPtr = ref.alloc(AVDictionaryPtrPtr);

libavformat.av_register_all();
libavformat.avformat_open_input(contextPtrPtr,"test.mov",null,null);
var contextHandle = contextPtrPtr.deref();
console.log(contextHandle);

OSXだとセグフォ

libavformatもnodeも64ビットな環境で動かしてみたら、

Segmentation fault: 11
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4