さんだすメモ

さメモ

技術ブログでは、ない・・・

  らぶ動画

はじめに

video_idに対応するものを1つ保存します。video_idは適当な名前です。補助的な作業が要らないので完成すれば簡単にできます。

手順

意味不明なドットは省略です。

ログインする

require 'net/https'
require 'openssl'

host_love = "https://.........com"
port_love = 443
https_love = Net::HTTP.new(host_love, port_love)
https_love.use_ssl = true
https_love.verify_mode = OpenSSL::SSL::VERIFY_PEER

# ログインする
response_love = https_love.start { |https|
  https_love.post('/user....?LoginForm', "plan_id=#{plan_id}&actmode=LoginDo&userno=#{userno}&password=#{password}")
}

# cookieを取得
header_love = {}
response_love.get_fields('Set-Cookie').each { |cookie|
  header_love['Cookie'] = cookie.split(';')[0]
}

動画の情報を見つける

# 動画のページにアクセスする
response_movie = https_love.get("/.../movie_#{"%02d" % video_id}.html", header_love)

# 存在しなければ飛ばす
if response_movie.code = "404"
  # 終了処理
end

# ページの本体
body_movie = response_movie.body.force_encoding('utf-8')

# タイトル
body_movie.match(/\<p\>(.*[^\s])(\s*)\|(\s*)([^\s].*)\<\/p\>/)
title = $1 + "_" + $4

HLSストリーミングだった場合

  • https://cc........../init?...
  • https://cc........../get_info?...

の2つを順番に調べます。

body_movie.match(/E....\.P.....\.embedkey="([^"]*)"/)
embedkey = $1

host_cc = "cc.........."
port_cc = 443
https_cc = Net::HTTP.new(host_cc, port_cc)
https_cc.use_ssl = true
https_cc.verify_mode = OpenSSL::SSL::VERIFY_PEER

# https://cc........../init?...
path_init = "/init"
query_init = "embedkey=#{embedkey}"
response_init = https_cc.start { |https|
  https.get("#{path_init}?#{query_init}")
}

# ハッシュを表す文字列が渡される
# 中でnullが使われているので、とりあえずnull = nilとしておく
null = nil
hash_init = eval(response_init.body)

# https://cc........../get_info?...
# initから得た情報を使ってアクセスする
path_get_info = "/get_info"
array_query_get_info = Array.new()
[:videotype,:host,:id_vhost,:id_contents].each { |key|
  array_query_get_info.push("#{key}=#{hash_init[key]}")
}
query_get_info = array_query_get_info.join('&')
response_get_info = https_cc.start { |https|
  https.get("#{path_get_info}?#{query_get_info}")
}

# ハッシュを表す文字列が渡される
# null = nil
hash_get_info = eval(response_get_info.body)

# get_info内にいくつかindex.m3u8があるが、その中からbitrateが高いものを選ぶ
url_m3u8 = ""
max_bitrate = 0
hash_get_info[:qualities].each { |hash|
  if max_bitrate < hash[:bitrate].to_i
    max_bitrate = hash[:bitrate].to_i
    url_m3u8 = hash[:url]
  end
}

# ffmpegを使ってmp4で保存
system "ffmpeg -i #{url_m3u8} -flags +loop+global_header -bsf:a aac_adtstoasc -movflags faststart -c copy \"C:/.../#{title}.mp4\""

mp4だった場合

ソースのURLが貼ってある場合です。

body_movie.match(/\<source src="([^"]*)"/)
url_mp4 = $1
system "ffmpeg -i #{url_mp4} -flags +loop+global_header -movflags faststart -c copy \"C:/.../#{title}.mp4\""

最後に

video_idは2桁まで0埋めの数字です。ffmpegのコマンドについてはよくわかっていないんですが、それでも便利ですね。