如何使用python获取该块中的总ETA时间和第一个ETA时间

时间:2017-05-15 02:03:27

标签: python python-2.7

我有一个文件,我需要获得每个块的总ETA时间和该块的第一个ETA时间实例。这是我的数据的一个例子。

*****START OF YOUTUBE*****

VIDEO

[download]   0.0% of 5.30MiB at 188.75KiB/s ETA 00:29
[download]   0.1% of 5.30MiB at 474.99KiB/s ETA 00:11
[download]   0.1% of 5.30MiB at 984.97KiB/s ETA 00:05
[download]   0.3% of 5.30MiB at  1.85MiB/s ETA 00:02
[download]   0.6% of 5.30MiB at 896.97KiB/s ETA 00:06
[download]   1.2% of 5.30MiB at 888.54KiB/s ETA 00:06
[download]   2.3% of 5.30MiB at 1006.45KiB/s ETA 00:05
[download]   4.7% of 5.30MiB at 916.41KiB/s ETA 00:05
[download]   9.4% of 5.30MiB at 863.70KiB/s ETA 00:05
[download]  18.9% of 5.30MiB at 847.73KiB/s ETA 00:05
[download]  34.2% of 5.30MiB at 843.72KiB/s ETA 00:04
[download]  49.7% of 5.30MiB at 840.54KiB/s ETA 00:03
[download]  65.0% of 5.30MiB at 839.77KiB/s ETA 00:02
[download]  80.5% of 5.30MiB at 839.71KiB/s ETA 00:01
[download]  95.9% of 5.30MiB at 839.39KiB/s ETA 00:00
[download] 100.0% of 5.30MiB at 842.74KiB/s ETA 00:00
[download] 100% of 5.30MiB in 00:06
AUDIO

[download]   0.0% of 2.99MiB at 112.61KiB/s ETA 00:27
[download]   0.1% of 2.99MiB at 311.43KiB/s ETA 00:09
[download]   0.2% of 2.99MiB at 682.19KiB/s ETA 00:04
[download]   0.5% of 2.99MiB at  1.32MiB/s ETA 00:02
[download]   1.0% of 2.99MiB at 943.80KiB/s ETA 00:03
[download]   2.1% of 2.99MiB at  1.17MiB/s ETA 00:02
[download]   4.1% of 2.99MiB at  1.31MiB/s ETA 00:02
[download]   8.3% of 2.99MiB at  1.59MiB/s ETA 00:01
[download]  16.7% of 2.99MiB at  1.37MiB/s ETA 00:01
[download]  33.4% of 2.99MiB at  1.03MiB/s ETA 00:01
[download]  60.8% of 2.99MiB at 942.47KiB/s ETA 00:01
[download]  88.1% of 2.99MiB at 908.52KiB/s ETA 00:00
[download] 100.0% of 2.99MiB at 897.53KiB/s ETA 00:00
[download] 100% of 2.99MiB in 00:03

MKV


*****END OF YOUTUBE*****

*****START OF YOUTUBE*****

VIDEO

[download]   0.0% of 41.89MiB at 251.26KiB/s ETA 02:55
[download]   0.0% of 41.89MiB at 612.01KiB/s ETA 01:10
[download]   0.0% of 41.89MiB at  1.23MiB/s ETA 00:34
[download]   0.0% of 41.89MiB at  2.33MiB/s ETA 00:18
[download]   0.1% of 41.89MiB at 783.21KiB/s ETA 00:54
[download]   0.1% of 41.89MiB at 959.46KiB/s ETA 00:44
[download]   0.3% of 41.89MiB at  1.16MiB/s ETA 00:36
[download]   0.6% of 41.89MiB at  1.45MiB/s ETA 00:28
[download]   1.2% of 41.89MiB at  1.15MiB/s ETA 00:35
[download]   2.4% of 41.89MiB at 986.82KiB/s ETA 00:42
[download]   4.4% of 41.89MiB at 912.91KiB/s ETA 00:44
[download]   6.3% of 41.89MiB at 877.92KiB/s ETA 00:45
[download]   8.2% of 41.89MiB at 870.89KiB/s ETA 00:45
[download]  10.2% of 41.89MiB at 865.95KiB/s ETA 00:44
[download]  12.1% of 41.89MiB at 862.33KiB/s ETA 00:43
[download]  14.1% of 41.89MiB at 859.01KiB/s ETA 00:42
[download]  16.1% of 41.89MiB at 857.18KiB/s ETA 00:42
[download]  18.0% of 41.89MiB at 855.34KiB/s ETA 00:41
[download]  20.0% of 41.89MiB at 850.76KiB/s ETA 00:40
[download]  21.9% of 41.89MiB at 850.71KiB/s ETA 00:39
[download]  23.9% of 41.89MiB at 850.61KiB/s ETA 00:38
[download]  25.9% of 41.89MiB at 850.12KiB/s ETA 00:37
[download]  27.8% of 41.89MiB at 849.74KiB/s ETA 00:36
[download]  29.8% of 41.89MiB at 849.08KiB/s ETA 00:35
[download]  31.7% of 41.89MiB at 848.78KiB/s ETA 00:34
[download]  33.7% of 41.89MiB at 846.31KiB/s ETA 00:33
[download]  35.6% of 41.89MiB at 844.42KiB/s ETA 00:32
[download]  37.5% of 41.89MiB at 844.68KiB/s ETA 00:31
[download]  39.5% of 41.89MiB at 844.87KiB/s ETA 00:30
[download]  41.5% of 41.89MiB at 844.76KiB/s ETA 00:29
[download]  43.4% of 41.89MiB at 844.93KiB/s ETA 00:28
[download]  45.4% of 41.89MiB at 844.77KiB/s ETA 00:27
[download]  47.4% of 41.89MiB at 843.37KiB/s ETA 00:26
[download]  49.2% of 41.89MiB at 843.65KiB/s ETA 00:25
[download]  51.2% of 41.89MiB at 843.66KiB/s ETA 00:24
[download]  53.2% of 41.89MiB at 843.50KiB/s ETA 00:23
[download]  55.2% of 41.89MiB at 843.46KiB/s ETA 00:22
[download]  57.1% of 41.89MiB at 843.19KiB/s ETA 00:21
[download]  59.1% of 41.89MiB at 843.22KiB/s ETA 00:20
[download]  61.0% of 41.89MiB at 842.88KiB/s ETA 00:19
[download]  63.0% of 41.89MiB at 843.00KiB/s ETA 00:18
[download]  65.0% of 41.89MiB at 842.80KiB/s ETA 00:17
[download]  66.9% of 41.89MiB at 842.91KiB/s ETA 00:16
[download]  68.9% of 41.89MiB at 842.09KiB/s ETA 00:15
[download]  70.8% of 41.89MiB at 842.40KiB/s ETA 00:14
[download]  72.8% of 41.89MiB at 840.95KiB/s ETA 00:13
[download]  74.6% of 41.89MiB at 840.38KiB/s ETA 00:12
[download]  76.5% of 41.89MiB at 840.77KiB/s ETA 00:11
[download]  78.5% of 41.89MiB at 840.21KiB/s ETA 00:10
[download]  80.4% of 41.89MiB at 840.45KiB/s ETA 00:09
[download]  82.4% of 41.89MiB at 840.65KiB/s ETA 00:08
[download]  84.4% of 41.89MiB at 840.80KiB/s ETA 00:07
[download]  86.4% of 41.89MiB at 840.35KiB/s ETA 00:06
[download]  88.3% of 41.89MiB at 840.73KiB/s ETA 00:05
[download]  90.3% of 41.89MiB at 840.32KiB/s ETA 00:04
[download]  92.2% of 41.89MiB at 840.69KiB/s ETA 00:03
[download]  94.2% of 41.89MiB at 840.31KiB/s ETA 00:02
[download]  96.1% of 41.89MiB at 840.64KiB/s ETA 00:01
[download]  98.1% of 41.89MiB at 840.26KiB/s ETA 00:00
[download] 100.0% of 41.89MiB at 840.32KiB/s ETA 00:00
[download] 100% of 41.89MiB in 00:51

AUDIO

[download]   0.0% of 10.35MiB at 114.98KiB/s ETA 01:32
[download]   0.0% of 10.35MiB at 317.99KiB/s ETA 00:33
[download]   0.1% of 10.35MiB at 694.85KiB/s ETA 00:15
[download]   0.1% of 10.35MiB at  1.37MiB/s ETA 00:07
[download]   0.3% of 10.35MiB at 846.04KiB/s ETA 00:12
[download]   0.6% of 10.35MiB at  1.14MiB/s ETA 00:09
[download]   1.2% of 10.35MiB at  1.29MiB/s ETA 00:07
[download]   2.4% of 10.35MiB at  1.71MiB/s ETA 00:05
[download]   4.8% of 10.35MiB at  2.00MiB/s ETA 00:04
[download]   9.7% of 10.35MiB at  1.17MiB/s ETA 00:07
[download]  17.7% of 10.35MiB at 985.70KiB/s ETA 00:08
[download]  25.3% of 10.35MiB at 940.66KiB/s ETA 00:08
[download]  33.3% of 10.35MiB at 916.90KiB/s ETA 00:07
[download]  41.4% of 10.35MiB at 900.34KiB/s ETA 00:06
[download]  49.3% of 10.35MiB at 885.27KiB/s ETA 00:06
[download]  56.9% of 10.35MiB at 880.58KiB/s ETA 00:05
[download]  65.0% of 10.35MiB at 876.17KiB/s ETA 00:04
[download]  73.0% of 10.35MiB at 868.96KiB/s ETA 00:03
[download]  80.7% of 10.35MiB at 867.56KiB/s ETA 00:02
[download]  88.7% of 10.35MiB at 865.95KiB/s ETA 00:01
[download]  96.7% of 10.35MiB at 862.35KiB/s ETA 00:00
[download] 100.0% of 10.35MiB at 862.21KiB/s ETA 00:00
[download] 100% of 10.35MiB in 00:12

MKV

*****END OF YOUTUBE*****

每个块的起点是视频到音频,然后另一个是音频到mkv。第一个块的样本输出应为:

First Block Video to Audio:
ETA TIME Instance = 00:29
Block ETA Time total = 01:35

Second Block Audio to Mkv:
ETA TIME Instance = 00:27
Block ETA Time total = 00:56

直到文件结束。

1 个答案:

答案 0 :(得分:1)

这是一个基本的解决方案:

from collections import deque
import re
import datetime

def print_block(first, last, first_eta, total_eta):
    print("First Block {} to {}:".format(first.capitalize(), last.capitalize()))
    print("ETA TIME Instance = {}".format(str(first_eta)))
    print("Block ETA Time total = {}".format(str(total_eta)))
    print()

def convert_to_timedelta(time_delta):
    blocks = time_delta.split(":")
    if len(blocks) == 2:
        return datetime.timedelta(minutes=int(blocks[0]), seconds=int(blocks[1]))
    else:
        return datetime.timedelta(hours=int(blocks[0]), minutes=int(blocks[1]), seconds=int(blocks[2]))


def process_chunk(chunk):
    queue = deque()
    first_eta = None
    total_eta = datetime.timedelta(hours=0,minutes=0, seconds=0)
    for line in chunk:
        # If line is a source change, add to queue.
        if line.isupper():
            queue.append(line)
            # If queue is full, pair sources and remove the first one.
            # Inner chunk can now be printed.
            if len(queue) == 2:
                first = queue.popleft() 
                last = queue[0]
                print_block(first, last, first_eta, total_eta)
                # Reset etas. 
                first_eta = None
                total_eta = datetime.timedelta(hours=0,minutes=0, seconds=0)
        else:
            # Find ETA **:** value. 
            m = re.search(r'ETA\s(.*)', line)
            if m is not None:
                time_delta = convert_to_timedelta(m.group(1))
                if first_eta == None:
                    first_eta = time_delta
                total_eta += time_delta

# Read file and split up into YOUTUBE blocks.
if __name__ == "__main__":
    chunk = []
    with open("file.txt", "r") as f:
        for line in f:
            # Process each youtube block seperately.
            if line.startswith("*****END OF YOUTUBE*****"):
                process_chunk(chunk)
                chunk = []
            else:
                line = line.rstrip()
                if line != '' and not line.startswith('*****START OF YOUTUBE*****'):
                    chunk.append(line.rstrip())

产生以下结果(根据您的示例):

First Block Video to Audio:
ETA TIME Instance = 0:00:29
Block ETA Time total = 0:01:29

First Block Audio to Mkv:
ETA TIME Instance = 0:00:27
Block ETA Time total = 0:00:53

First Block Video to Audio:
ETA TIME Instance = 0:02:55
Block ETA Time total = 0:28:22

First Block Audio to Mkv:
ETA TIME Instance = 0:01:32
Block ETA Time total = 0:04:01

请注意,total_eta的计算方法是仅对前缀为ETA的值求和(并且不包括您在计算中执行的总时间线)。如果要更改该行为,只需更改正则表达式。

不是最高效的内存版本(它是O(n^2)),但我认为可读性比速度更重要。可以通过逐行处理块来修改它以在O(n)中运行(而不是在读取整个块之后)。