ShowTimeCode

説明

ビデオクリップにタイムコード(00:00:00.00 etc.)を追加する関数です。フレームレートと現在のフレーム番号から時間を計算して、クリップ上に表示します。実際のタイムコードとは、若干の誤差が生じる可能性があります。

AviSynth 内蔵の Subtitle フィルタを使用する ShowTimeCode と、SubtitleEx プラグイン(AviSynth Filter Collection から入手可能)を使用する ShowTimeCodeEx の 2 つがあります。

注意: AviSynth 内蔵の ShowSMPTE フィルタを使って、この関数と同様の効果を得ることが可能です。

作者

  • にーやん
  • 2ちゃんねる「お前らのショボイAvisynthスクリプト貼ってくださいpart2」の 253 さん

コード

# Use this, when you use internal Subtitle filter
function ShowTimeCode(clip c, int "x", int "y", string "font", int "size",
\ int "text_color", int "halo_color", int "align", int "spc",
\ float "offset", bool "nowtime", string "mode", int "ms_digit"){
    # Set default values.
    global stc_x = default(x, 8)
    global stc_y = default(y, 18)
    global stc_font = default(font, "Arial")
    global stc_size = default(size, 18)
    global stc_text_color = default(text_color, $FFFF00)
    global stc_halo_color = default(halo_color, $000000)
    global stc_align = default(align, 4)
    global stc_spc = default(spc, 0)
    global stc_mode = default(mode, "hh:mm:ss:ms")
    global stc_ms_digit = default(ms_digit, 2)
    offset = default(offset, 0.0)
    nowtime = default(nowtime, false)
   
    # Throw error when ms_digit isn't between 1 and 3.
    Assert((stc_ms_digit>=1)&&(stc_ms_digit<=3), "Error: ms_digit must be between 1 and 3.")
   
    # Get the current system time.
    system_time = (nowtime == true) ? Int(Value(Time("%H")))*60*60 +
    \ Int(Value(Time("%M")))*60 + Int(Value(Time("%S"))) : NOP
   
    # If nowtime is true, add system_time as offset.
    global stc_offset = (nowtime == true) ? offset + system_time : offset
   
    # Evaluate time in each frames (in seconds) and show as GetTimeCode's format.
    return ScriptClip(c, "get_time = (current_frame)/FrameRate()" +
    \ "total_time = GetTimeCode(get_time, stc_offset, stc_mode, stc_ms_digit)" +
    \ "Subtitle(total_time, stc_x, stc_y, current_frame, current_frame,
    \ stc_font, stc_size, stc_text_color, stc_halo_color, stc_align, stc_spc)")
}

# Use this, if you want to use SubtitleEx plugin
function ShowTimeCodeEx(clip c, int "x", int "y", string "font",
\ string "effects", int "size", int "textcolor", int "halocolor",
\ float "offset", bool "nowtime", string "mode", int "ms_digit"){
    # Set default values.
    global stcex_x = default(x, Round(-Width(c)*0.11))
    global stcex_y = default(y, Round(-Height(c)*0.17))
    global stcex_font = default(font, "Arial")
    global stcex_effects = default(effects, "b")
    global stcex_size = default(size, 36)
    global stcex_textcolor = default(textcolor, $00FFFFFF)
    global stcex_halocolor = default(halocolor, $00000000)
    global stcex_mode = default(mode, "hh:mm:ss:ms")
    global stcex_ms_digit = default(ms_digit, 2)
    offset = default(offset, 0.0)
    nowtime = default(nowtime, false)
   
    # Throw error when ms_digit isn't between 1 and 3.
    Assert((stcex_ms_digit>=1)&&(stcex_ms_digit<=3), "Error: ms_digit must be between 1 and 3.")
   
    # Get the current system time.
    system_time = (nowtime == true) ? Int(Value(Time("%H")))*60*60 +
    \ Int(Value(Time("%M")))*60 + Int(Value(Time("%S"))) : NOP
   
    # If nowtime is true, add system_time as offset.
    global stcex_offset = (nowtime == true) ? offset + system_time : offset
   
    # Evaluate time in each frames (in seconds) and show as GetTimeCode's format.
    return ScriptClip(c, "get_time = (current_frame)/FrameRate()" +
    \ "total_time = GetTimeCode(get_time, stcex_offset, stcex_mode, stcex_ms_digit)" +
    \ "SubtitleEx(total_time, stcex_x, stcex_y, current_frame, current_frame,
    \ stcex_font, stcex_effects, stcex_size, stcex_textcolor, stcex_halocolor)")
}

# Used inside ShowTimeCode/ShowTimeCodeEx
function GetTimeCode(float get_time, float offset, string mode, int ms_digit){
    # Calculate mili-seconds and convert to string.
    factor = Pow(10, ms_digit) # 小数点以下を繰り上げするための倍数(10 の ms_digit 乗)
    ms_int = Round(get_time*factor) % Int(factor) # get_time の factor 倍したものを factor で割った余り
    ms = String(ms_int, "%0"+String(ms_digit)+".0f") # 書式指定して文字列型に変換
   
    # Calculate integer seconds
    int_time = Round(get_time-(Value(ms)/factor)) # 元の時間から小数点以下を引いた数を丸める
   
    # Calculate seconds and convert to string.
    ss = String(int_time%60)
    ss = RightStr("0" + ss, 2)
   
    # Calculate minutes and convert to string.
    mm = String((int_time/60)%60)
    mm = RightStr("0" + mm, 2)
   
    # Calculate hours and convert to string.
    hh = ((int_time/60)/60)%60
    hh_multiplier = hh/24
    hh = (hh >= 24) ? hh - (24*hh_multiplier) : hh
    hh = (hh < 10) ? "0" + String(hh) : String(hh)
   
    # Decide what kind of mark is used as separation, when showing ms.
    separation = (RightStr(mode, 2) == "ms") ? LeftStr(RightStr(mode, 3), 1) : NOP
   
    # Generate timecode.
    timecode =
    \ (mode == "ts") ? String(int_time) :
    \ ((mode == "ts.ms")||(mode == "ts:ms")) ? String(int_time) + separation + ms :
    \ (mode == "ss") ? ss :
    \ ((mode == "ss.ms")||(mode == "ss:ms")) ? ss + separation + ms :
    \ ((mode == "mm:ss.ms")||(mode == "mm:ss:ms")) ? mm + ":" + ss + separation + ms :
    \ ((mode == "hh:mm:ss.ms")||(mode == "hh:mm:ss:ms")) ?
    \ hh + ":" + mm + ":" + ss + separation + ms :
    \ (mode == "mm:ss") ? mm + ":" + ss :
    \ (mode == "hh:mm:ss") ? hh + ":" + mm + ":" + ss :
    \ "Invalid"
   
    Assert(timecode != "Invalid" , """Error: such "mode" doesn't exist.""")
   
    # Return time code as string.
    return timecode
}

シンタックス

ShowTimeCode

function ShowTimeCode(clip c, int "x", int "y", string "font", int "size",
\ int "text_color", int "halo_color", int "align", int "spc",
\ float "offset", bool "nowtime", string "mode", int "ms_digit")

ShowTimeCodeEx

ShowTimeCodeEx(clip c, int "x", int "y", string "font",
\ string "effects", int "size", int "textcolor", int "halocolor",
\ float "offset", bool "nowtime", string "mode", int "ms_digit")

パラメータ

Subtitle フィルタと同じ働きをするパラメータ

  • c: ビデオクリップ
  • x, y: タイムコードの表示位置(x-y 座標)。
  • font: フォント。
  • size: フォントの大きさ。
  • text_color: フォントの色。
  • halo_color: フォントの縁の色。

ShowTimeCode / ShowTimeCodeEx で追加されたパラメータ

  • offset: オフセット。デフォルト: 0.0。
  • nowtime: システム時間を取得するかどうか。デフォルト: false。
  • mode: 表示モード。以下のいずれか:
    • "ts"
    • "ts.ms" または "ts:ms"
    • "ss"
    • "ss.ms" または "ss:ms"
    • "mm:ss.ms" または "mm:ss:ms"
    • "hh:mm:ss.ms" または "hh:mm:ss:ms"(デフォルト)
    • "mm:ss"
    • "hh:mm:ss"
  • ms_digit: ミリ秒単位において表示する桁の数。デフォルト: 2。

ShowTimeCode のみ

  • align: デフォルトの表示位置。Subtitle フィルタの align。
  • spc: フォントの間隔。Subtitle フィルタの spc。

ShowTimeCodeEx のみ

  • effects: SubtitleEx の effects(デフォルト: "b")。
    • "b" - 太字
    • "i" - イタリック体
    • "u" - アンダーライン
    • "c" - 中央寄せ
    • "s" - ソフトぼかし
    • "g" - ガウシアンぼかし
    • "e" - エンボスフィルタ
    • "l" - ラプラシアンフィルタ(エッジ検出)

使用例

BlankClip(46, 448, 336, "YV12", fps=15)
ShowTimeCode(x=Width()/2, y=Height()/2, font="7barP", size=48, align=5, ms_digit=3)

動画サンプル

関連ページ

最終更新日時: 2014-03-12 (水) 23:40:37 (3696d)