1

我正在尝试解析以下字符串(实际上这是一个列表,无论如何......)。

+ title 1:
  + Main Feature
  + vts 1, ttn 1, cells 0->47 (3155896 blocks)
  + duration: 02:06:44
  + size: 720x576, pixel aspect: 64/45, display aspect: 1.78, 25.000 fps
  + autocrop: 0/0/2/0
  + chapters:
    + 1: cells 0->0, 107073 blocks, duration 00:04:06
    + 2: cells 1->1, 104931 blocks, duration 00:04:16
    + 3: cells 2->2, 43568 blocks, duration 00:01:39
    + 4: cells 3->3, 56955 blocks, duration 00:02:27
    + 5: cells 4->4, 50952 blocks, duration 00:01:50
    + 6: cells 5->5, 30214 blocks, duration 00:01:10
    + 7: cells 6->6, 35935 blocks, duration 00:01:26
    + 8: cells 7->7, 99999 blocks, duration 00:04:16
    + 9: cells 8->8, 34513 blocks, duration 00:01:23
    + 10: cells 9->9, 51879 blocks, duration 00:02:14
    + 11: cells 10->10, 43780 blocks, duration 00:01:51
    + 12: cells 11->11, 70721 blocks, duration 00:03:06
    + 13: cells 12->12, 28275 blocks, duration 00:01:06
    + 14: cells 13->13, 105788 blocks, duration 00:04:15
    + 15: cells 14->14, 66071 blocks, duration 00:02:29
    + 16: cells 15->15, 45366 blocks, duration 00:01:55
    + 17: cells 16->16, 54022 blocks, duration 00:02:12
    + 18: cells 17->17, 28551 blocks, duration 00:01:10
    + 19: cells 18->18, 72501 blocks, duration 00:02:54
    + 20: cells 19->19, 111458 blocks, duration 00:04:13
    + 21: cells 20->20, 52493 blocks, duration 00:01:58
    + 22: cells 21->21, 76352 blocks, duration 00:03:17
    + 23: cells 22->22, 72562 blocks, duration 00:03:11
    + 24: cells 23->23, 43124 blocks, duration 00:01:48
    + 25: cells 24->24, 134155 blocks, duration 00:05:01
    + 26: cells 25->25, 61777 blocks, duration 00:02:19
    + 27: cells 26->26, 61983 blocks, duration 00:02:26
    + 28: cells 27->27, 112308 blocks, duration 00:04:40
    + 29: cells 28->28, 44673 blocks, duration 00:01:32
    + 30: cells 29->29, 104886 blocks, duration 00:03:55
    + 31: cells 30->30, 53520 blocks, duration 00:02:10
    + 32: cells 31->31, 75386 blocks, duration 00:03:09
    + 33: cells 32->32, 90275 blocks, duration 00:03:36
    + 34: cells 33->33, 64572 blocks, duration 00:02:29
    + 35: cells 34->34, 26363 blocks, duration 00:01:07
    + 36: cells 35->35, 26959 blocks, duration 00:01:06
    + 37: cells 36->36, 45596 blocks, duration 00:01:49
    + 38: cells 37->37, 74188 blocks, duration 00:03:03
    + 39: cells 38->38, 31681 blocks, duration 00:01:20
    + 40: cells 39->39, 136832 blocks, duration 00:05:40
    + 41: cells 40->40, 84664 blocks, duration 00:03:32
    + 42: cells 41->41, 88245 blocks, duration 00:03:33
    + 43: cells 42->42, 79290 blocks, duration 00:03:15
    + 44: cells 43->43, 34200 blocks, duration 00:01:18
    + 45: cells 44->44, 17481 blocks, duration 00:00:43
    + 46: cells 45->45, 96702 blocks, duration 00:03:26
    + 47: cells 46->46, 34126 blocks, duration 00:01:21
    + 48: cells 47->47, 88951 blocks, duration 00:03:56
  + audio tracks:
    + 1, English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 384000bps
    + 2, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps
    + 3, Deutsch (AC3) (5.1 ch) (iso639-2: deu), 48000Hz, 384000bps
    + 4, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps
  + subtitle tracks:
    + 1, English (iso639-2: eng) (Bitmap)(VOBSUB)
    + 2, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
    + 3, Italiano (iso639-2: ita) (Bitmap)(VOBSUB)
    + 4, Francais (iso639-2: fra) (Bitmap)(VOBSUB)
    + 5, Norsk (iso639-2: nor) (Bitmap)(VOBSUB)
    + 6, Dansk (iso639-2: dan) (Bitmap)(VOBSUB)
    + 7, Svenska (iso639-2: swe) (Bitmap)(VOBSUB)
    + 8, Suomi (iso639-2: fin) (Bitmap)(VOBSUB)
    + 9, Polish (iso639-2: pol) (Bitmap)(VOBSUB)
    + 10, Czech (iso639-2: ces) (Bitmap)(VOBSUB)
    + 11, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
+ title 2:
  + vts 1, ttn 2, cells 0->0 (4259 blocks)
  + duration: 00:00:12
  + size: 720x576, pixel aspect: 64/45, display aspect: 1.78, 25.000 fps
  + autocrop: 8/0/0/2
  + chapters:
    + 1: cells 0->0, 4259 blocks, duration 00:00:12
  + audio tracks:
    + 1, English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 384000bps
    + 2, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps
    + 3, Deutsch (AC3) (5.1 ch) (iso639-2: deu), 48000Hz, 384000bps
    + 4, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps
  + subtitle tracks:
    + 1, English (iso639-2: eng) (Bitmap)(VOBSUB)
    + 2, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
    + 3, Italiano (iso639-2: ita) (Bitmap)(VOBSUB)
    + 4, Francais (iso639-2: fra) (Bitmap)(VOBSUB)
    + 5, Norsk (iso639-2: nor) (Bitmap)(VOBSUB)
    + 6, Dansk (iso639-2: dan) (Bitmap)(VOBSUB)
    + 7, Svenska (iso639-2: swe) (Bitmap)(VOBSUB)
    + 8, Suomi (iso639-2: fin) (Bitmap)(VOBSUB)
    + 9, Polish (iso639-2: pol) (Bitmap)(VOBSUB)
    + 10, Czech (iso639-2: ces) (Bitmap)(VOBSUB)
    + 11, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
+ title 3:
  + vts 1, ttn 3, cells 0->0 (37474 blocks)
  + duration: 00:01:51
  + size: 720x576, pixel aspect: 64/45, display aspect: 1.78, 25.000 fps
  + autocrop: 0/0/2/2
  + chapters:
    + 1: cells 0->0, 37474 blocks, duration 00:01:51
  + audio tracks:
    + 1, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps
  + subtitle tracks:
    + 1, English (iso639-2: eng) (Bitmap)(VOBSUB)
    + 2, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
    + 3, Italiano (iso639-2: ita) (Bitmap)(VOBSUB)
    + 4, Francais (iso639-2: fra) (Bitmap)(VOBSUB)
    + 5, Norsk (iso639-2: nor) (Bitmap)(VOBSUB)
    + 6, Dansk (iso639-2: dan) (Bitmap)(VOBSUB)
    + 7, Svenska (iso639-2: swe) (Bitmap)(VOBSUB)
    + 8, Suomi (iso639-2: fin) (Bitmap)(VOBSUB)
    + 9, Polish (iso639-2: pol) (Bitmap)(VOBSUB)
    + 10, Czech (iso639-2: ces) (Bitmap)(VOBSUB)
    + 11, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
+ title 4:
  + vts 2, ttn 1, cells 0->0 (6817 blocks)
  + duration: 00:00:19
  + size: 720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps
  + autocrop: 4/0/12/0
  + chapters:
    + 1: cells 0->0, 6817 blocks, duration 00:00:19
  + audio tracks:
    + 1, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps
  + subtitle tracks:
    + 1, English (iso639-2: eng) (Bitmap)(VOBSUB)
    + 2, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
    + 3, Italiano (iso639-2: ita) (Bitmap)(VOBSUB)
    + 4, Francais (iso639-2: fra) (Bitmap)(VOBSUB)
    + 5, Norsk (iso639-2: nor) (Bitmap)(VOBSUB)
    + 6, Dansk (iso639-2: dan) (Bitmap)(VOBSUB)
    + 7, Svenska (iso639-2: swe) (Bitmap)(VOBSUB)
    + 8, Suomi (iso639-2: fin) (Bitmap)(VOBSUB)
    + 9, Polish (iso639-2: pol) (Bitmap)(VOBSUB)
    + 10, Czech (iso639-2: ces) (Bitmap)(VOBSUB)
    + 11, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
+ title 5:
  + vts 2, ttn 2, cells 0->0 (12051 blocks)
  + duration: 00:00:34
  + size: 720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps
  + autocrop: 0/0/10/0
  + chapters:
    + 1: cells 0->0, 12051 blocks, duration 00:00:34
  + audio tracks:
    + 1, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps
  + subtitle tracks:
    + 1, English (iso639-2: eng) (Bitmap)(VOBSUB)
    + 2, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
    + 3, Italiano (iso639-2: ita) (Bitmap)(VOBSUB)
    + 4, Francais (iso639-2: fra) (Bitmap)(VOBSUB)
    + 5, Norsk (iso639-2: nor) (Bitmap)(VOBSUB)
    + 6, Dansk (iso639-2: dan) (Bitmap)(VOBSUB)
    + 7, Svenska (iso639-2: swe) (Bitmap)(VOBSUB)
    + 8, Suomi (iso639-2: fin) (Bitmap)(VOBSUB)
    + 9, Polish (iso639-2: pol) (Bitmap)(VOBSUB)
    + 10, Czech (iso639-2: ces) (Bitmap)(VOBSUB)
    + 11, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
+ title 6:
  + vts 2, ttn 3, cells 0->0 (12246 blocks)
  + duration: 00:00:35
  + size: 720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps
  + autocrop: 78/80/6/4
  + chapters:
    + 1: cells 0->0, 12246 blocks, duration 00:00:35
  + audio tracks:
    + 1, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps
  + subtitle tracks:
    + 1, English (iso639-2: eng) (Bitmap)(VOBSUB)
    + 2, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
    + 3, Italiano (iso639-2: ita) (Bitmap)(VOBSUB)
    + 4, Francais (iso639-2: fra) (Bitmap)(VOBSUB)
    + 5, Norsk (iso639-2: nor) (Bitmap)(VOBSUB)
    + 6, Dansk (iso639-2: dan) (Bitmap)(VOBSUB)
    + 7, Svenska (iso639-2: swe) (Bitmap)(VOBSUB)
    + 8, Suomi (iso639-2: fin) (Bitmap)(VOBSUB)
    + 9, Polish (iso639-2: pol) (Bitmap)(VOBSUB)
    + 10, Czech (iso639-2: ces) (Bitmap)(VOBSUB)
    + 11, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
+ title 7:
  + vts 2, ttn 4, cells 0->0 (11983 blocks)
  + duration: 00:00:34
  + size: 720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps
  + autocrop: 78/80/8/4
  + chapters:
    + 1: cells 0->0, 11983 blocks, duration 00:00:34
  + audio tracks:
    + 1, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps
  + subtitle tracks:
    + 1, English (iso639-2: eng) (Bitmap)(VOBSUB)
    + 2, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
    + 3, Italiano (iso639-2: ita) (Bitmap)(VOBSUB)
    + 4, Francais (iso639-2: fra) (Bitmap)(VOBSUB)
    + 5, Norsk (iso639-2: nor) (Bitmap)(VOBSUB)
    + 6, Dansk (iso639-2: dan) (Bitmap)(VOBSUB)
    + 7, Svenska (iso639-2: swe) (Bitmap)(VOBSUB)
    + 8, Suomi (iso639-2: fin) (Bitmap)(VOBSUB)
    + 9, Polish (iso639-2: pol) (Bitmap)(VOBSUB)
    + 10, Czech (iso639-2: ces) (Bitmap)(VOBSUB)
    + 11, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
+ title 8:
  + vts 2, ttn 5, cells 0->0 (34523 blocks)
  + duration: 00:01:37
  + size: 720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps
  + autocrop: 0/0/10/8
  + chapters:
    + 1: cells 0->0, 34523 blocks, duration 00:01:37
  + audio tracks:
    + 1, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps
  + subtitle tracks:
    + 1, English (iso639-2: eng) (Bitmap)(VOBSUB)
    + 2, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)
    + 3, Italiano (iso639-2: ita) (Bitmap)(VOBSUB)
    + 4, Francais (iso639-2: fra) (Bitmap)(VOBSUB)
    + 5, Norsk (iso639-2: nor) (Bitmap)(VOBSUB)
    + 6, Dansk (iso639-2: dan) (Bitmap)(VOBSUB)
    + 7, Svenska (iso639-2: swe) (Bitmap)(VOBSUB)
    + 8, Suomi (iso639-2: fin) (Bitmap)(VOBSUB)
    + 9, Polish (iso639-2: pol) (Bitmap)(VOBSUB)
    + 10, Czech (iso639-2: ces) (Bitmap)(VOBSUB)
    + 11, Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)

我想在这里使用 re 但也许有一个简单的解决方案?我的大脑今天看到了太多的代码。

如果所有信息都存储在单个 dict() 中,那将是最好的

{'title1': 
    {'duration': '02:06:44', 
     'audio_tracks': 
         [{'1': 'English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 384000bps' },
         {'2': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps' },
         ...]},
     'subtitle_tracks':
         [{'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)' },
         {'2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)' },
         ...]},
     }
}

像这样的东西。也许有人有一些提示如何解析:

+ something:
    + some: info,
    + something:,
        + 1: indented information

事物?

4

2 回答 2

3

我有这个用于各种目的的缩进文本解析器。在这里,我将其调整为 ffmpeg 格式:

import sys
import re

root = ['root', []]
path = [root]
last_indent = -1
for line in sys.stdin:
    line = line.rstrip()
    if not line:
        break

    # calculate indent
    iline = line
    line = line.lstrip(' ')
    indent = (len(iline) - len(line))/2

    line = line.lstrip('+ ')
    bits = re.split('[:,]', line, 1)
    key = bits[0]
    try:
        content = bits[1].strip()
    except IndexError:
        content = None

    if not content:
        # root node
        content = []

    if indent <= last_indent:
        del path[indent - last_indent - 1:]
    cur = [key, content]
    path[-1][-1].append(cur)
    path.append(cur)
    last_indent = indent

pprint(root)

输出:

['root',
 [['title 1',
   [['Main Feature', []],
    ['vts 1', 'ttn 1, cells 0->47 (3155896 blocks)'],
    ['duration', '02:06:44'],
    ['size',
     '720x576, pixel aspect: 64/45, display aspect: 1.78, 25.000 fps'],
    ['autocrop', '0/0/2/0'],
    ['chapters',
     [['1', 'cells 0->0, 107073 blocks, duration 00:04:06'],
      ['2', 'cells 1->1, 104931 blocks, duration 00:04:16'],
      ['3', 'cells 2->2, 43568 blocks, duration 00:01:39'],
      ['4', 'cells 3->3, 56955 blocks, duration 00:02:27'],
      ['5', 'cells 4->4, 50952 blocks, duration 00:01:50'],
      ['6', 'cells 5->5, 30214 blocks, duration 00:01:10'],
      ['7', 'cells 6->6, 35935 blocks, duration 00:01:26'],
      ['8', 'cells 7->7, 99999 blocks, duration 00:04:16'],
      ['9', 'cells 8->8, 34513 blocks, duration 00:01:23'],
      ['10', 'cells 9->9, 51879 blocks, duration 00:02:14'],
      ['11', 'cells 10->10, 43780 blocks, duration 00:01:51'],
      ['12', 'cells 11->11, 70721 blocks, duration 00:03:06'],
      ['13', 'cells 12->12, 28275 blocks, duration 00:01:06'],
      ['14', 'cells 13->13, 105788 blocks, duration 00:04:15'],
      ['15', 'cells 14->14, 66071 blocks, duration 00:02:29'],
      ['16', 'cells 15->15, 45366 blocks, duration 00:01:55'],
      ['17', 'cells 16->16, 54022 blocks, duration 00:02:12'],
      ['18', 'cells 17->17, 28551 blocks, duration 00:01:10'],
      ['19', 'cells 18->18, 72501 blocks, duration 00:02:54'],
      ['20', 'cells 19->19, 111458 blocks, duration 00:04:13'],
      ['21', 'cells 20->20, 52493 blocks, duration 00:01:58'],
      ['22', 'cells 21->21, 76352 blocks, duration 00:03:17'],
      ['23', 'cells 22->22, 72562 blocks, duration 00:03:11'],
      ['24', 'cells 23->23, 43124 blocks, duration 00:01:48'],
      ['25', 'cells 24->24, 134155 blocks, duration 00:05:01'],
      ['26', 'cells 25->25, 61777 blocks, duration 00:02:19'],
      ['27', 'cells 26->26, 61983 blocks, duration 00:02:26'],
      ['28', 'cells 27->27, 112308 blocks, duration 00:04:40'],
      ['29', 'cells 28->28, 44673 blocks, duration 00:01:32'],
      ['30', 'cells 29->29, 104886 blocks, duration 00:03:55'],
      ['31', 'cells 30->30, 53520 blocks, duration 00:02:10'],
      ['32', 'cells 31->31, 75386 blocks, duration 00:03:09'],
      ['33', 'cells 32->32, 90275 blocks, duration 00:03:36'],
      ['34', 'cells 33->33, 64572 blocks, duration 00:02:29'],
      ['35', 'cells 34->34, 26363 blocks, duration 00:01:07'],
      ['36', 'cells 35->35, 26959 blocks, duration 00:01:06'],
      ['37', 'cells 36->36, 45596 blocks, duration 00:01:49'],
      ['38', 'cells 37->37, 74188 blocks, duration 00:03:03'],
      ['39', 'cells 38->38, 31681 blocks, duration 00:01:20'],
      ['40', 'cells 39->39, 136832 blocks, duration 00:05:40'],
      ['41', 'cells 40->40, 84664 blocks, duration 00:03:32'],
      ['42', 'cells 41->41, 88245 blocks, duration 00:03:33'],
      ['43', 'cells 42->42, 79290 blocks, duration 00:03:15'],
      ['44', 'cells 43->43, 34200 blocks, duration 00:01:18'],
      ['45', 'cells 44->44, 17481 blocks, duration 00:00:43'],
      ['46', 'cells 45->45, 96702 blocks, duration 00:03:26'],
      ['47', 'cells 46->46, 34126 blocks, duration 00:01:21'],
      ['48', 'cells 47->47, 88951 blocks, duration 00:03:56']]],
    ['audio tracks',
     [['1', 'English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 384000bps'],
      ['2',
       'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'],
      ['3', 'Deutsch (AC3) (5.1 ch) (iso639-2: deu), 48000Hz, 384000bps'],
      ['4',
       'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps']]],
    ['subtitle tracks',
     [['1', 'English (iso639-2: eng) (Bitmap)(VOBSUB)'],
      ['2', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)'],
      ['3', 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)'],
      ['4', 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)'],
      ['5', 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)'],
      ['6', 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)'],
      ['7', 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)'],
      ['8', 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)'],
      ['9', 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'],
      ['10', 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)'],
      ['11', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)']]]]],
  ['title 2',
   [['vts 1', 'ttn 2, cells 0->0 (4259 blocks)'],
    ['duration', '00:00:12'],
    ['size',
     '720x576, pixel aspect: 64/45, display aspect: 1.78, 25.000 fps'],
    ['autocrop', '8/0/0/2'],
    ['chapters', [['1', 'cells 0->0, 4259 blocks, duration 00:00:12']]],
    ['audio tracks',
     [['1', 'English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 384000bps'],
      ['2',
       'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'],
      ['3', 'Deutsch (AC3) (5.1 ch) (iso639-2: deu), 48000Hz, 384000bps'],
      ['4',
       'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps']]],
    ['subtitle tracks',
     [['1', 'English (iso639-2: eng) (Bitmap)(VOBSUB)'],
      ['2', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)'],
      ['3', 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)'],
      ['4', 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)'],
      ['5', 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)'],
      ['6', 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)'],
      ['7', 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)'],
      ['8', 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)'],
      ['9', 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'],
      ['10', 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)'],
      ['11', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)']]]]],
  ['title 3',
   [['vts 1', 'ttn 3, cells 0->0 (37474 blocks)'],
    ['duration', '00:01:51'],
    ['size',
     '720x576, pixel aspect: 64/45, display aspect: 1.78, 25.000 fps'],
    ['autocrop', '0/0/2/2'],
    ['chapters', [['1', 'cells 0->0, 37474 blocks, duration 00:01:51']]],
    ['audio tracks',
     [['1',
       'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps']]],
    ['subtitle tracks',
     [['1', 'English (iso639-2: eng) (Bitmap)(VOBSUB)'],
      ['2', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)'],
      ['3', 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)'],
      ['4', 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)'],
      ['5', 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)'],
      ['6', 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)'],
      ['7', 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)'],
      ['8', 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)'],
      ['9', 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'],
      ['10', 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)'],
      ['11', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)']]]]],
  ['title 4',
   [['vts 2', 'ttn 1, cells 0->0 (6817 blocks)'],
    ['duration', '00:00:19'],
    ['size',
     '720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps'],
    ['autocrop', '4/0/12/0'],
    ['chapters', [['1', 'cells 0->0, 6817 blocks, duration 00:00:19']]],
    ['audio tracks',
     [['1',
       'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps']]],
    ['subtitle tracks',
     [['1', 'English (iso639-2: eng) (Bitmap)(VOBSUB)'],
      ['2', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)'],
      ['3', 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)'],
      ['4', 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)'],
      ['5', 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)'],
      ['6', 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)'],
      ['7', 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)'],
      ['8', 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)'],
      ['9', 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'],
      ['10', 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)'],
      ['11', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)']]]]],
  ['title 5',
   [['vts 2', 'ttn 2, cells 0->0 (12051 blocks)'],
    ['duration', '00:00:34'],
    ['size',
     '720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps'],
    ['autocrop', '0/0/10/0'],
    ['chapters', [['1', 'cells 0->0, 12051 blocks, duration 00:00:34']]],
    ['audio tracks',
     [['1',
       'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps']]],
    ['subtitle tracks',
     [['1', 'English (iso639-2: eng) (Bitmap)(VOBSUB)'],
      ['2', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)'],
      ['3', 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)'],
      ['4', 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)'],
      ['5', 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)'],
      ['6', 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)'],
      ['7', 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)'],
      ['8', 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)'],
      ['9', 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'],
      ['10', 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)'],
      ['11', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)']]]]],
  ['title 6',
   [['vts 2', 'ttn 3, cells 0->0 (12246 blocks)'],
    ['duration', '00:00:35'],
    ['size',
     '720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps'],
    ['autocrop', '78/80/6/4'],
    ['chapters', [['1', 'cells 0->0, 12246 blocks, duration 00:00:35']]],
    ['audio tracks',
     [['1',
       'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps']]],
    ['subtitle tracks',
     [['1', 'English (iso639-2: eng) (Bitmap)(VOBSUB)'],
      ['2', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)'],
      ['3', 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)'],
      ['4', 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)'],
      ['5', 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)'],
      ['6', 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)'],
      ['7', 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)'],
      ['8', 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)'],
      ['9', 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'],
      ['10', 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)'],
      ['11', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)']]]]],
  ['title 7',
   [['vts 2', 'ttn 4, cells 0->0 (11983 blocks)'],
    ['duration', '00:00:34'],
    ['size',
     '720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps'],
    ['autocrop', '78/80/8/4'],
    ['chapters', [['1', 'cells 0->0, 11983 blocks, duration 00:00:34']]],
    ['audio tracks',
     [['1',
       'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps']]],
    ['subtitle tracks',
     [['1', 'English (iso639-2: eng) (Bitmap)(VOBSUB)'],
      ['2', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)'],
      ['3', 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)'],
      ['4', 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)'],
      ['5', 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)'],
      ['6', 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)'],
      ['7', 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)'],
      ['8', 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)'],
      ['9', 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'],
      ['10', 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)'],
      ['11', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)']]]]],
  ['title 8',
   [['vts 2', 'ttn 5, cells 0->0 (34523 blocks)'],
    ['duration', '00:01:37'],
    ['size',
     '720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps'],
    ['autocrop', '0/0/10/8'],
    ['chapters', [['1', 'cells 0->0, 34523 blocks, duration 00:01:37']]],
    ['audio tracks',
     [['1',
       'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps']]],
    ['subtitle tracks',
     [['1', 'English (iso639-2: eng) (Bitmap)(VOBSUB)'],
      ['2', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)'],
      ['3', 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)'],
      ['4', 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)'],
      ['5', 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)'],
      ['6', 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)'],
      ['7', 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)'],
      ['8', 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)'],
      ['9', 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'],
      ['10', 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)'],
      ['11', 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)']]]]]]]

您必须针对所需的输出对其进行调整,但这应该给您一个开始。

于 2013-02-16T17:35:46.677 回答
0

http://ffmpeg.org/ffprobe.htmlhttps://reviews.handbrake.fr/r/224/都解决了。

所以我想出了自己的解析算法。在这里(l 必须是一个 list() - 只需将 Handbrake 输出除以 '\n'):

解析 HandBrakeCLI 输出

def getHandBrakeInformation(l):
    currentTitle = None
    titleDict = {}
    returnDict = {}
    for line in l:
        if line.startswith('+ title'):
            titleNum = line.rstrip(':').split(' ')
            currentTitle = "%s_%s" % (titleNum[1], titleNum[2])
            titleDict[currentTitle] = []
            returnDict[currentTitle] = {}
        else:
            titleDict[currentTitle].append(line[2:])

    currentTrack = None
    for title in titleDict:
        for key in titleDict[title]:
            if key.startswith('+ duration:'):
                duration = key.split(' ')
                duration = duration[2]
                returnDict[title]['duration'] = duration
                titleDict[title].remove(key)
            elif key.startswith('+ audio') or key.startswith('+ subtitle'):
                currentTrack = key[2:-1].replace(' ', '_')
                returnDict[title][currentTrack] = {}
            elif currentTrack is not None and not key.startswith('+'):
                currentTrackValueSplit = key[4:].split(',', 1)
                returnDict[title][currentTrack][currentTrackValueSplit[0].strip()] = currentTrackValueSplit[1].strip()
            else:
                currentTrack = None
    return returnDict

这里是输出:

{'title_1': {'audio_tracks': {'1': 'English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 384000bps',
   '2': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps',
   '3': 'Deutsch (AC3) (5.1 ch) (iso639-2: deu), 48000Hz, 384000bps',
   '4': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '02:06:44',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_2': {'audio_tracks': {'1': 'English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 384000bps',
   '2': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps',
   '3': 'Deutsch (AC3) (5.1 ch) (iso639-2: deu), 48000Hz, 384000bps',
   '4': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:00:12',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_3': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:01:51',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_4': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:00:19',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_5': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:00:34',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_6': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:00:35',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_7': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:00:34',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}},
 'title_8': {'audio_tracks': {'1': 'English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps'},
  'duration': '00:01:37',
  'subtitle_tracks': {'1': 'English (iso639-2: eng) (Bitmap)(VOBSUB)',
   '10': 'Czech (iso639-2: ces) (Bitmap)(VOBSUB)',
   '11': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '2': 'Deutsch (iso639-2: deu) (Bitmap)(VOBSUB)',
   '3': 'Italiano (iso639-2: ita) (Bitmap)(VOBSUB)',
   '4': 'Francais (iso639-2: fra) (Bitmap)(VOBSUB)',
   '5': 'Norsk (iso639-2: nor) (Bitmap)(VOBSUB)',
   '6': 'Dansk (iso639-2: dan) (Bitmap)(VOBSUB)',
   '7': 'Svenska (iso639-2: swe) (Bitmap)(VOBSUB)',
   '8': 'Suomi (iso639-2: fin) (Bitmap)(VOBSUB)',
   '9': 'Polish (iso639-2: pol) (Bitmap)(VOBSUB)'}}}
于 2013-02-19T09:16:05.623 回答