Created
May 3, 2021 17:05
-
-
Save RGBA-CRT/6c39d229b993ef39bc9f894f6b446625 to your computer and use it in GitHub Desktop.
ts録画ファイルから.ts.program.txtを生成するpythonスクリプト(TVRemotePlusに喰わせる用)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @echo off | |
| for /f "delims=" %%i in ('dir /b .\*.ts') do ( | |
| py make_program_txt.py "%%i" | |
| ) | |
| pause |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf8 -*- | |
| import subprocess | |
| import csv | |
| # import chardet | |
| import io | |
| import csv | |
| from datetime import datetime as dt | |
| import datetime | |
| import locale | |
| import sys | |
| import traceback | |
| locale.setlocale(locale.LC_TIME, 'ja_JP.UTF-8') | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print("usage: {} input.ts".format(sys.argv[0])) | |
| exit() | |
| filename = sys.argv[1] | |
| file_pos = 10 | |
| write_mode = "xb" # Todo: wbへのスイッチもオプションで用意したい | |
| rplsinfo = subprocess.Popen( | |
| ['rplsinfo', filename, '-C', '-fukdtpzaoscnbieg', '-y', '-j', '-F', str(file_pos)], shell=True, stdout=subprocess.PIPE) | |
| ts_info, stderr_data = rplsinfo.communicate() | |
| # charinfo = chardet.detect(ts_info) | |
| # ts_info_txt = ts_info.decode(encoding=charinfo['encoding']) | |
| ts_info_txt = ts_info.decode(encoding="CP932") | |
| if(rplsinfo.returncode): | |
| print("failed:") | |
| print(filename) | |
| print(ts_info_txt) | |
| return | |
| ts_info_stream = io.StringIO() | |
| ts_info_stream.write(ts_info_txt) | |
| ts_info_stream.seek(0) | |
| csv_table = csv.reader(ts_info_stream, delimiter=',') | |
| for row in csv_table: | |
| file_path = row[1] | |
| start = get_start_datetime(row[3], row[4]) | |
| end = get_end_datetime(start, row[5]) | |
| broadcaster = row[10] | |
| program_name = row[12] | |
| program_short_info = row[13] | |
| program_detail_info = row[14] | |
| program_genre = row[15] | |
| # i = 0 | |
| # for column in row: | |
| # print("[{}]: {}".format(i, column)) | |
| # i += 1 | |
| datestr = get_program_time_text(start, end) | |
| # output | |
| f = open(file_path+".program.txt", write_mode) | |
| out_text(f, datestr) | |
| out_text(f, broadcaster) | |
| out_text(f, program_name) | |
| out_text(f, "") | |
| out_text(f, program_short_info) | |
| out_text(f, "") | |
| out_text(f, "詳細情報") | |
| out_text(f, program_detail_info.replace("\r\n\r\n", "\r\n")) | |
| out_text(f, "") | |
| out_text(f, "") | |
| # TvRemotePlusはジャンルが現れた時点でパースが止まる | |
| # ジャンル文字列は雑パース | |
| out_text(f, "ジャンル : ") | |
| out_text(f, program_genre.replace( | |
| " ", "\r\n").replace(" 〔", " - ").replace("〕", "")) | |
| # 値はダミー | |
| out_text(f, "") | |
| out_text(f, "映像 : 1080i(1125i)、アスペクト比16:9 パンベクトルなし、ダミー情報") | |
| out_text(f, "音声 : 2/0モード(ステレオ)、ダミー情報") | |
| out_text(f, "ステレオ") | |
| out_text(f, "サンプリングレート : 48kHz") | |
| out_text(f, "") | |
| out_text(f, "OriginalNetworkID:dummy(dummy_hex)") | |
| out_text(f, "TransportStreamID:dummy(dummy_hex)") | |
| out_text(f, "ServiceID:dummy(dummy_hex)") | |
| out_text(f, "EventID:dummy(dummy_hex)") | |
| def get_start_datetime(date, start_time): | |
| date = date + " " + start_time | |
| return dt.strptime(date, '%Y/%m/%d %H:%M:%S') | |
| def get_end_datetime(start_datetime, dur): | |
| h, m, s = (int(x) for x in dur.split(":")) | |
| dur_td = datetime.timedelta(hours=h, minutes=m, seconds=s) | |
| return start_datetime+dur_td | |
| def get_program_time_text(start, end): | |
| # 文字コードの問題でstrftimeが使えないので | |
| weekday_label = ["月", "火", "水", "木", "金", "土", "日"] | |
| return "{:04d}/{:02d}/{:02d}({}) {:02d}:{:02d}~{:02d}:{:02d}".format( | |
| start.year, start.month, start.day, | |
| weekday_label[start.weekday()], | |
| start.hour, start.minute, | |
| end.hour, end.minute | |
| ) | |
| def out_text(f, text): | |
| f.write((text+"\r\n").encode("CP932")) | |
| try: | |
| main() | |
| except Exception as e: | |
| print("exception:") | |
| print(sys.argv) | |
| print(e) | |
| print(traceback.format_exc()) | |
| print() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
depends