2
0

More than 1 year has passed since last update.

EPGStation で WebAPI を使って余分な TS ファイルを削除

Last updated at Posted at 2023-01-23

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

トランスコーディング作業元の TS ファイルを削除するのは今までスクリプトでやってきたが、

「拡張子の異なる同じファイルを削除するワンライナー」
https://qiita.com/nanbuwks/items/7f09b7aaed9ae17c4c1e

それだと EPGStation 上での DB と齟齬が生じる。

「EPGStation での録画ルールを一括変更」
https://qiita.com/nanbuwks/items/a6cb0635c947da0090ab

では python で EPGStationの WebAPI を使いましたが、
同様にして EPGStation の録画ファイルを削除するようにしてみます。

環境

  • 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. 不要な録画ファイルがあるか判定
  3. 不要な録画ファイルを削除

録画リスト取得、不要な録画ファイルがあるか判定

以下のようにして取得

$ curl -X 'GET'   'http://localhost:8888/api/recorded?isHalfWidth=false&offset=4100&limit=1'   -H 'acceptapplication/json'

取得したもの

{"records":[{"id":1621,"channelId":3239123608,"startAt":1665246600000,"endAt":1665248400000,"name":"ポプテピピック TVアニメーション作品第二シリーズ #2","isRecording":false,"isEncoding":false,"isProtected":false,"ruleId":3,"programId":323912360844375,"description":"これは、夢と希望の物語(ただのクソ)―。","genre1":7,"subGenre1":0,"videoType":"mpeg2","videoResolution":"1080i","videoStreamContent":1,"videoComponentType":179,"audioSamplingRate":48000,"audioComponentType":3,"thumbnails":[1548],"videoFiles":[{"id":2938,"name":"TS","filename":"2022年10月09日01時30分00秒-ポプテピピック TVアニメーション作品第二シリーズ #2.m2ts","type":"ts","size":2480183608},{"id":2958,"name":"H.264","filename":"2022年10月09日01時30分00秒-ポプテピピック TVアニメーション作品第二シリーズ #2.mp4","type":"encoded","size":688218663}]}],"total":5715}

この中で必要なのは

  • "id":1621
  • "videoFiles":[{"id":2938,"name":"TS","filename":"2022年10月09日01時30分00秒-ポプテピピック TVアニメーション作品第二シリーズ #2.m2ts","type":"ts","size":2480183608},{"id":2958,"name":"H.264","filename":"2022年10月09日01時30分00秒-ポプテピピック TVアニメーション作品第二シリーズ #2.mp4","type":"encoded","size":688218663}]}]

上記から、録画ファイルが2つあるので TS形式ファイル

{"id":2938,"name":"TS","filename":"2022年10月09日01時30分00秒-ポプテピピック TVアニメーション作品第二シリーズ #2.m2ts","type":"ts","size":2480183608}

を削除する。

不要な録画ファイルを削除

$ curl -X 'DELETE' \
>   'http://localhost:8888/api/videos/2938' \
>   -H 'accept: application/json'
{"code":200}nanbuwks@nanbuwks-PRIMERGY-TX1310-M3:~/Downloads/docker/docker-mirakurun-epgstation$ 

うまくいったようです。

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"
urldelete = "http://192.168.42.100:8888/api/videos/"
# 録画一覧を得る
response = urllib.request.urlopen(urlget)
jsonData = json.load(response)
# 録画ごとにループ
for jsonObj in jsonData["records"]:
    # "videoFilesを取り出し
    flagEncoded=False;
    flagTs=False;
    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 ( True==flagEncoded and True==flagTs):
    # "videoFiles":"type":"encoded"と"type":"ts"が両方ある場合
      print(idTs,":",end="")
      if ( 0 ==  sizeEncoded ):
        #  念の為エンコードが失敗している場合を除外
        print("delete cancel.because encoded file size is zero.",end="")
        print(":",nameTs)
      else:
        # time.sleep(10)
        print("delete...",end="")
        url=urldelete+str(idTs)
        headers = {"Content-Type": "application/json"}
        # print (json.dumps(s))
        res=urllib.request.Request(url,json.dumps(jsonObj).encode(),headers,method="DELETE")
        try:
          with urllib.request.urlopen(res) as f:
              pass
          print(f.status,end="")
          if (200==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)


実行

.
.
.
9086 :delete cancel.because encoded file size is zero.: 2023年01月03日13時40分00秒-アニメ 不滅のあなたへ シーズン2(10)「共鳴」[字].m2ts
9084 :delete...200 OK: 2023年01月03日13時15分00秒-アニメ 不滅のあなたへ シーズン2(9)「拡(ひろ)がる意識」[字].m2ts
9082 :delete...200 OK: 2023年01月03日12時50分00秒-アニメ 不滅のあなたへ シーズン2(8)「夢の先」[字].m2ts
9079 :delete...200 OK: 2023年01月03日12時25分00秒-アニメ 不滅のあなたへ シーズン2(7)「罰とゆるし」[字].m2ts
9078 :delete...200 OK: 2023年01月03日12時00分00秒-アニメ 不滅のあなたへ シーズン2(6)「異端の徒」[字].m2ts
9073 :delete... Error 500 at: 2023年01月03日09時30分00秒-映画「漁港の肉子ちゃん」[字].m2ts
.
.
.

割と、500エラーが出ています。削除前後にウェイトをかけたりしましたが改善せず。サーバ側でエンコードしていてこんな状況だったので、たてこんでいたかな?
image.png

サーバのファイルを確認すると500エラーが出ていても無事に削除できているようなので、この処理でOKとしています。

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