<commit_before>module Bulldog
  module Processor
    class Ffmpeg < Base
      class << self
        attr_accessor :ffmpeg_path
      end

      def initialize(*args)
        super
        @operation = nil
        @arguments = style_list_map
        @still_frame_callbacks = style_list_map
      end

      def process
        return if styles.empty?
        super
        run_ffmpeg
        run_still_frame_callbacks
      end

      def process_style(*args)
        super
        run_default_operation
      end

      def use_threads(num_threads)
        operate '-threads', num_threads
      end

      def encode(params={})
        @operation = :encode
        params = style.attributes.merge(params)
        parse_video_option(params)
        parse_audio_option(params)
        style_option '-vcodec', params[:video_codec]
        style_option '-acodec', params[:audio_codec]
        preset_option '-vpre', params[:video_preset]
        preset_option '-apre', params[:audio_preset]
        operate '-s', attachment.dimensions(style.name).join('x') if params[:size]
        style_option '-r', params[:frame_rate]
        style_option '-b', params[:video_bit_rate]
        style_option '-ar', params[:sampling_rate]
        style_option '-ab', params[:audio_bit_rate]
        style_option '-ac', params[:channels]
        operate '-deinterlace' if params[:deinterlaced]
        style_option '-pix_fmt', params[:pixel_format]
        style_option '-b_strategy', params[:b_strategy]
        style_option '-bufsize', params[:buffer_size]
        style_option '-coder', params[:coder]
        style_option '-v', params[:verbosity]
        style_option '-flags', params[:flags]
        preset_option '-spre', params[:subtitle_preset]
        style_option '-y', output_file(style.name)
      end

      def record_frame(params={}, &block)
        @operation = :record_frame
        params = style.attributes.merge(params)
        operate '-vframes', 1
        operate '-ss', params[:position] || attachment.duration.to_i / 2
        operate '-f', 'image2'
        operate '-vcodec', params[:codec] || default_frame_codec(params)

        if (attribute = params[:assign_to])
          basename = "recorded_frame.#{params[:format]}"
          output_path = record.send(attribute).interpolate_path(:original, :basename => basename)
          @still_frame_callbacks[style] << lambda do
            file = SavedFile.new(output_path, :file_name => basename)
            record.update_attribute(attribute, file)
          end
        else
          output_path = output_file(style.name)
        end

        operate '-y', output_path
        if block
          @still_frame_callbacks[style] << lambda{instance_exec(output_path, &block)}
        end<commit_msg>Move -spre argument up with other preset arguments for ffmpeg.<commit_after>module Bulldog
  module Processor
    class Ffmpeg < Base
      class << self
        attr_accessor :ffmpeg_path
      end

      def initialize(*args)
        super
        @operation = nil
        @arguments = style_list_map
        @still_frame_callbacks = style_list_map
      end

      def process
        return if styles.empty?
        super
        run_ffmpeg
        run_still_frame_callbacks
      end

      def process_style(*args)
        super
        run_default_operation
      end

      def use_threads(num_threads)
        operate '-threads', num_threads
      end

      def encode(params={})
        @operation = :encode
        params = style.attributes.merge(params)
        parse_video_option(params)
        parse_audio_option(params)
        style_option '-vcodec', params[:video_codec]
        style_option '-acodec', params[:audio_codec]
        preset_option '-vpre', params[:video_preset]
        preset_option '-apre', params[:audio_preset]
        preset_option '-spre', params[:subtitle_preset]
        operate '-s', attachment.dimensions(style.name).join('x') if params[:size]
        style_option '-r', params[:frame_rate]
        style_option '-b', params[:video_bit_rate]
        style_option '-ar', params[:sampling_rate]
        style_option '-ab', params[:audio_bit_rate]
        style_option '-ac', params[:channels]
        operate '-deinterlace' if params[:deinterlaced]
        style_option '-pix_fmt', params[:pixel_format]
        style_option '-b_strategy', params[:b_strategy]
        style_option '-bufsize', params[:buffer_size]
        style_option '-coder', params[:coder]
        style_option '-v', params[:verbosity]
        style_option '-flags', params[:flags]
        style_option '-y', output_file(style.name)
      end

      def record_frame(params={}, &block)
        @operation = :record_frame
        params = style.attributes.merge(params)
        operate '-vframes', 1
        operate '-ss', params[:position] || attachment.duration.to_i / 2
        operate '-f', 'image2'
        operate '-vcodec', params[:codec] || default_frame_codec(params)

        if (attribute = params[:assign_to])
          basename = "recorded_frame.#{params[:format]}"
          output_path = record.send(attribute).interpolate_path(:original, :basename => basename)
          @still_frame_callbacks[style] << lambda do
            file = SavedFile.new(output_path, :file_name => basename)
            record.update_attribute(attribute, file)
          end
        else
          output_path = output_file(style.name)
        end

        operate '-y', output_path
        if block
          @still_frame_callbacks[style] << lambda{instance_exec(output_path, &block)}
        end
{'fraction_non_alphanumeric': 0.1073680417942713, 'fraction_numerical': 0.0010808863267879661, 'mean_word_length': 2.5318066157760812, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '102157', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1590, 'n_words': 338}