import logging
import os

from ffmpeg.ffprobe import FFprobeInfoCommand, FFprobeFrameCommand
from ffmpeg import jinja_env


class FfmpegAction:

    def __init__(self, conf: dict):
        self._conf = conf

    def run(self, input_url: str, action_params: dict, out_rel_path: str=None):
        logging.info('Using FFprobe to collect input file parameters...')
        ffprobe_info = FFprobeInfoCommand(self._conf['ffprobe_path'])
        input_params = ffprobe_info.exec(input_url, show_programs=False)
        logging.debug('Input parameters: {}'.format(input_params))

        logging.info('Searching for video and audio streams...')
        v_streams = {}
        a_streams = {}
        for s in input_params['streams']:
            if s['codec_type'] == 'video':
                v_streams[s['index']] = s
            elif s['codec_type'] == 'audio':
                a_streams[s['index']] = s
        logging.info('Found {} video stream(s) and {} audio stream(s)'.format(len(v_streams), len(a_streams)))
        logging.debug('Video stream index(es): {}, audio stream indexes: {}'.format(
            ', '.join([str(v) for v in v_streams.keys()]),
            ', '.join([str(a) for a in a_streams.keys()])))

        logging.info('Collecting frames metadata from video streams...')
        ffprobe_frame = FFprobeFrameCommand(self._conf['ffprobe_path'])
        for n, vs in enumerate(v_streams.values()):
            v_frame_meta = ffprobe_frame.exec(input_url, 'v:{}'.format(n), '%+#1')['frames'][0]
            v_streams[v_frame_meta['stream_index']]['first_frame_meta'] = v_frame_meta

        profile_template_name = action_params['profile']
        logging.info('Loading profile template {}'.format(profile_template_name))
        template = jinja_env.get_template(profile_template_name)
        logging.info('Building profile template rendering context...')
        context = {
            'in_filename': os.path.splitext(os.path.split(input_url)[1])[0],
            'in_format': input_params['format'],
            'in_v_streams': v_streams,
            'in_a_streams': a_streams,
        }
        logging.debug('Rendering context: {}'.format(context))
        profile_data = template.render(context)
        logging.info('Profile rendering complete')
        logging.debug('Rendered profile: {}'.format(profile_data))

@@ -138,16 +138,65 @@
 ction:%0A%0A
+    INTERLACING_DETERMINATION_FRAME_COUNT = 100%0A%0A
     def 
@@ -1282,53 +1282,58 @@
 fo('
-Collecting frames metadata from video streams
+Decoding some frames to determine interlacing mode
 ...'
@@ -1478,20 +1478,20 @@
 v_frame_
-meta
+list
  = ffpro
@@ -1508,43 +1508,157 @@
 xec(
-input_url, 'v:%7B%7D'.format(n), '%25+#1'
+%0A                input_url,%0A                'v:%7B%7D'.format(n),%0A                '%25+#%7B%7D'.format(self.INTERLACING_DETERMINATION_FRAME_COUNT)%0A            
 )%5B'f
@@ -1664,19 +1664,16 @@
 frames'%5D
-%5B0%5D
 %0A       
@@ -1681,82 +1681,48 @@
     
-v_streams%5Bv_frame_meta%5B'stream_index'%5D%5D%5B'first_frame_meta'%5D = v_frame_meta
+interlaced = None%0A            tff = None
 %0A%0A  

Interlacing or progressive - that is the question. The Beginning