1
2

More than 1 year has passed since last update.

EPGStation で WebAPI を使ってエンコード自動化

Last updated at Posted at 2023-01-26

EPGStation での録画ファイル管理その2。

TS ファイルで録画したファイルをエンコードしたい。
EPGStation 上で管理したいののだが、 GUI でイチイチするのではなく自動化したい。

「EPGStation で WebAPI を使って余分な TS ファイルを削除」
https://qiita.com/nanbuwks/items/bab77ef5c61b5e74c905
「EPGStation での録画ルールを一括変更」
https://qiita.com/nanbuwks/items/a6cb0635c947da0090ab

では python で EPGStationの WebAPI を使いましたが、
同様にして EPGStation 上での TS ファイルを一括でエンコーディングします。

環境

  • Version of EPGStation: 2.6.20
  • Version of Mirakurun: 3.9.0-rc.2
  • OS:Ubuntu Linux 20.04
  • Architecture: x64

「PLEX PX-Q3PE4 で docker-mirakurun-epgstation を使いたい」
https://qiita.com/nanbuwks/items/640ee4405e1fdd2ca497
こちらで構築したものです。

作戦

  1. 録画リスト取得
  2. TSファイルだけの録画があるか判定
  3. エンコーディング予約

録画リスト取得、TSファイルだけの録画があるか判定

import urllib.request
import json
import time

# limit を適宜変更のこと
urlget = "http://192.168.42.100:8888/api/recorded?isHalfWidth=false&offset=0&limit=10000"
# 録画一覧を得る
response = urllib.request.urlopen(urlget)
jsonData = json.load(response)
# 録画ごとにループ
for jsonObj in jsonData["records"]:
    # "videoFilesを取り出し
    flagEncoded=False;
    flagTs=False;
    recordid=jsonObj["id"]
    for videoFileObj in jsonObj["videoFiles"]:
      if ("encoded" == videoFileObj["type"]):
          flagEncoded=True;
          #sizeEncoded=videoFileObj["size"]
      if ("ts" == videoFileObj["type"]):
          flagTs=True;
          idTs=videoFileObj["id"]
          nameTs=videoFileObj["filename"]
    if ( False==flagEncoded and True==flagTs):
    # "videoFiles":"type":"encoded"が存在しなくて "type":"ts"がある場合
      print(recordid,":",idTs,":",nameTs)

これを実行すると以下のように一覧が取得できた

.
.
.
4831 : 8121 : 2022年12月20日14時25分00秒-名曲アルバム「花のワルツ~“くるみ割り人形”から~」チャイコフスキー作曲.m2ts
4816 : 8092 : 2022年12月20日03時02分00秒-空からクルージング特別編「ヨーロッパの街や村」.m2ts
4813 : 8080 : 2022年12月20日00時49分00秒-BLEACH 千年血戦篇 第11話「EVERYTHING BUT THE RAIN」[字].m2ts
4803 : 8066 : 2022年12月19日23時00分00秒-ゴールデンカムイ(第四期) #41「シネマトグラフ」[再].m2ts
4804 : 8067 : 2022年12月19日23時00分00秒-ゴールデンカムイ(第四期)[再]第四十一話「シネマトグラフ」.m2ts
4802 : 8065 : 2022年12月19日22時50分00秒-BS世界のドキュメンタリー▽レーガンvsゴルバチョフ中距離核ミサイル全廃条約[二][字].m2ts
.
.
.

エンコーディング予約

http://:/api/debug 画面にて Try it out を選んで生成した以下のスクリプト

$ curl -X 'POST'   'http://localhost:8888/api/encode'   -H 'accept: application/json'   -H 'Content-Type: application/json'   -d '{
  "recordedId": 0,
  "sourceVideoFileId": 0,
  "parentDir": "string",
  "directory": "string",
  "isSaveSameDirectory": true,
  "mode": "string",
  "removeOriginal": true
}'

に先に取得した


4831 : 8121 : 2022年12月20日14時25分00秒-名曲アルバム「花のワルツ~“くるみ割り人形”から~」チャイコフスキー作曲.m2ts

の 4831 : 8121 を使って以下のようにした。

_current$ curl -X 'POST'   'http://localhost:8888/api/encode'   -H 'accept: application/json'   -H 'Content-Type: application/json'   -d '{
  "recordedId": 4831,
  "sourceVideoFileId": 8121,
  "parentDir": "recorded",
  "directory": "",
  "isSaveSameDirectory": true,
  "mode": "H.264",
  "removeOriginal": true
}'
{"encodeId":462}

parentDir や directory は isSaveSameDirectory が true なので指定しなくてもいいかなと思ったけど、parentDir を指定しておかないとうまくいきませんでした。

実行すると以下のように正常にエンコードがなされた。

image.png

image.png

これを上記の Python にくみこんで

#import requests
import urllib.request
import json
import time

# limit を適宜変更のこと
urlget = "http://192.168.42.100:8888/api/recorded?isHalfWidth=false&offset=0&limit=10000"
urlencode = "http://192.168.42.100:8888/api/encode/"
jsontemplate={"recordedId": 0,"sourceVideoFileId":0,"parentDir":"","directory":"","isSaveSameDirectory":True,"mode":"H.264","removeOriginal":True }


# 録画一覧を得る
response = urllib.request.urlopen(urlget)
jsonData = json.load(response)
# 録画ごとにループ
i=0
for jsonObj in jsonData["records"]:
    # "videoFilesを取り出し
    flagEncoded=False;
    flagTs=False;
    recordid=jsonObj["id"]
    for videoFileObj in jsonObj["videoFiles"]:
      if ("encoded" == videoFileObj["type"]):
          flagEncoded=True;
          #sizeEncoded=videoFileObj["size"]
      if ("ts" == videoFileObj["type"]):
          flagTs=True;
          idTs=videoFileObj["id"]
          nameTs=videoFileObj["filename"]
    if ( False==flagEncoded and True==flagTs):
    # "videoFiles":"type":"encoded"が存在しなくて "type":"ts"がある場合
      print(recordid,":",idTs,end=":")
      # recordedId を入力
      jsontemplate["recordedId"]=recordid
      # TSファイルのIDを入力
      jsontemplate["sourceVideoFileId"]=idTs
      # 保管場所を入力
      jsontemplate["parentDir"]="recorded"
      headers = {"Content-Type": "application/json"}
      res=urllib.request.Request(urlencode,json.dumps(jsontemplate).encode(),headers,method="POST")
      try:
          with urllib.request.urlopen(res) as f:
              pass
          print(f.status,end="")
          if (201==f.status):
            print(" OK",end="")
            print(":",nameTs)
          else:
            print(" Error",end=" ")
            print(f.reason)
            print(" at:",nameTs)
      except urllib.error.HTTPError as err:
          print(" Error",end=" ")
          print(err.code,end=" ")
          print("at:",nameTs)
      except urllib.error.URLError as err:
          print(" Error",end="")
          print(err.reason)
          print("at:",nameTs)

実行

6139 : 10632:201 OK: 2023年01月22日15時10分00秒-BS世界のドキュメンタリー「マミー・フット!おばあちゃんはサッカー選手」[二][字][再].m2ts
6131 : 10590:201 OK: 2023年01月22日04時20分00秒-名曲アルバム「交響曲“告別”」ハイドン作曲.m2ts
6129 : 10584:201 OK: 2023年01月22日03時05分00秒-空からクルージング特別編「フランス・ロワール川を下る」.m2ts
6097 : 10533:201 OK: 2023年01月21日15時59分00秒-空からクルージング特別編「イタリア・シチリア島の古代遺跡」.m2ts
6079 : 10472:201 OK: 2023年01月21日03時10分00秒-空からクルージング特別編「スコットランド・ハイランド横断紀行」.m2ts
6060 : 10448:201 OK: 2023年01月21日00時00分00秒-[字]ベストヒットUSA 「レッド・ホット・チリ・ペッパーズ、ジェイコブ・コリアー」.m2ts
.
.
.

うまくいきました。

特殊条件のチェック

エンコード後、削除するように設定しているけど、もしエンコード中のものをダブってエンコード指定するとどうなるかな? エンコードファイルを上書きしてしまうかな? でもエンコードソースの TSファイルがなくなるので 2つ目がエンコード失敗で、エンコードファイルも消えてしまうかな?

image.png

$ ls -ag 2023年01月22日15時10分00秒*
-rw-r--r-- 1 video   90439728  1月 26 15:13 '2023年01月22日15時10分00秒-BS世界のドキュメンタリー「マミー・フット!おばあちゃんはサッカー選手」[二][字][再](1).mp4'
-rw-r--r-- 1 video 6345864988  1月 22 16:00 '2023年01月22日15時10分00秒-BS世界のドキュメンタリー「マミー・フット!おばあちゃんはサッカー選手」[二][字][再].m2ts'
-rw-r--r-- 1 video  933406314  1月 26 15:10 '2023年01月22日15時10分00秒-BS世界のドキュメンタリー「マミー・フット!おばあちゃんはサッカー選手」[二][字][再].mp4'

tsファイルは消えませんでしたし、エンコードファイルも上書きしませんでした。

3回重なってエンコード指定し、エンコード終了したらこのようになりました。

image.png

TSファイルはファイラーで確認すると削除されていました。

1
2
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
1
2