<commit_before>package lib

import (
	"errors"
	"strconv"

	"github.com/3d0c/gmf"
	"github.com/snickers/snickers/db"
	"github.com/snickers/snickers/types"
)

func addStream(job types.Job, codecName string, oc *gmf.FmtCtx, ist *gmf.Stream) (int, int, error) {
	var cc *gmf.CodecCtx
	var ost *gmf.Stream

	codec, err := gmf.FindEncoder(codecName)
	if err != nil {
		return 0, 0, err
	}

	if ost = oc.NewStream(codec); ost == nil {
		return 0, 0, errors.New("unable to create stream in output context")
	}
	defer gmf.Release(ost)

	if cc = gmf.NewCodecCtx(codec); cc == nil {
		return 0, 0, errors.New("unable to create codec context")
	}
	defer gmf.Release(cc)

	if oc.IsGlobalHeader() {
		cc.SetFlag(gmf.CODEC_FLAG_GLOBAL_HEADER)
	}

	if codec.IsExperimental() {
		cc.SetStrictCompliance(gmf.FF_COMPLIANCE_EXPERIMENTAL)
	}

	if cc.Type() == gmf.AVMEDIA_TYPE_AUDIO {
		bitrate, err := strconv.Atoi(job.Preset.Audio.Bitrate)
		if err != nil {
			return 0, 0, err
		}
		cc.SetBitRate(bitrate)

		gop, err := strconv.Atoi(job.Preset.Video.GopSize)
		if err != nil {
			return 0, 0, err
		}
		cc.SetGopSize(gop)

		cc.SetSampleFmt(ist.CodecCtx().SampleFmt())
		cc.SetSampleRate(ist.CodecCtx().SampleRate())
		cc.SetChannels(ist.CodecCtx().Channels())
		cc.SelectChannelLayout()
		cc.SelectSampleRate()
	}

	if cc.Type() == gmf.AVMEDIA_TYPE_VIDEO {
		cc.SetTimeBase(gmf.AVR{Num: 1, Den: 25})
		if job.Preset.Video.Codec == "h264" {
			if job.Preset.Video.Profile == "baseline" {
				cc.SetProfile(gmf.FF_PROFILE_H264_BASELINE)
			} else if job.Preset.Video.Profile == "main" {
				cc.SetProfile(gmf.FF_PROFILE_H264_MAIN)
			} else if job.Preset.Video.Profile == "high" {
				cc.SetProfile(gmf.FF_PROFILE_H264_HIGH)
			}
		}
		width, err := strconv.Atoi(job.Preset.Video.Width)
		if err != nil {
			return 0, 0, err
		}
		height, err := strconv.Atoi(job.Preset.Video.Height)
		if err != nil {
			return 0, 0, err
		}

		bitrate, err := strconv.Atoi(job.Preset.Video.Bitrate)
		if err != nil {
			return 0, 0, err
		}

		cc.SetBitRate(bitrate)
		cc.SetDimension(width, height)
		cc.SetPixFmt(ist.CodecCtx().PixFmt())
	}

	if err := cc.Open(nil); err != nil {
		return 0, 0, err
	}

	ost.SetCodecCtx(cc)

	return ist.Index(), ost.Index(), nil
}

// FFMPEGEncode function is responsible for encoding the file
func FFMPEGEncode(jobID string) error {
	gmf.LogSetLevel(gmf.AV_LOG_FATAL)
	dbInstance, _ := db.GetDatabase()
	job, _ := dbInstance.RetrieveJob(jobID)
	srcFileName := job.LocalSource
	dstFileName := job.LocalDestination
	stMap := make(map[int]int, 0)
	var lastDelta int64

	inputCtx, err := gmf.NewInputCtx(srcFileName)
	if err != nil {
		return err
	}
	defer inputCtx.CloseInputAndRelease()

	outputCtx, err := gmf.NewOutputCtx(dstFileName)
	if err != nil {
		return err
	}
	defer outputCtx.CloseOutputAndRelease()

	job.Status = types.JobEncoding
	job.Details = "0%"
	dbInstance.UpdateJob(job.ID, job)<commit_msg>ffmpeg_encoder: create a helper to return output resolution<commit_after>package lib

import (
	"errors"
	"strconv"

	"github.com/3d0c/gmf"
	"github.com/snickers/snickers/db"
	"github.com/snickers/snickers/types"
)

func addStream(job types.Job, codecName string, oc *gmf.FmtCtx, ist *gmf.Stream) (int, int, error) {
	var cc *gmf.CodecCtx
	var ost *gmf.Stream

	codec, err := gmf.FindEncoder(codecName)
	if err != nil {
		return 0, 0, err
	}

	if ost = oc.NewStream(codec); ost == nil {
		return 0, 0, errors.New("unable to create stream in output context")
	}
	defer gmf.Release(ost)

	if cc = gmf.NewCodecCtx(codec); cc == nil {
		return 0, 0, errors.New("unable to create codec context")
	}
	defer gmf.Release(cc)

	if oc.IsGlobalHeader() {
		cc.SetFlag(gmf.CODEC_FLAG_GLOBAL_HEADER)
	}

	if codec.IsExperimental() {
		cc.SetStrictCompliance(gmf.FF_COMPLIANCE_EXPERIMENTAL)
	}

	if cc.Type() == gmf.AVMEDIA_TYPE_AUDIO {
		bitrate, err := strconv.Atoi(job.Preset.Audio.Bitrate)
		if err != nil {
			return 0, 0, err
		}
		cc.SetBitRate(bitrate)

		gop, err := strconv.Atoi(job.Preset.Video.GopSize)
		if err != nil {
			return 0, 0, err
		}
		cc.SetGopSize(gop)

		cc.SetSampleFmt(ist.CodecCtx().SampleFmt())
		cc.SetSampleRate(ist.CodecCtx().SampleRate())
		cc.SetChannels(ist.CodecCtx().Channels())
		cc.SelectChannelLayout()
		cc.SelectSampleRate()
	}

	if cc.Type() == gmf.AVMEDIA_TYPE_VIDEO {
		cc.SetTimeBase(gmf.AVR{Num: 1, Den: 25})
		if job.Preset.Video.Codec == "h264" {
			if job.Preset.Video.Profile == "baseline" {
				cc.SetProfile(gmf.FF_PROFILE_H264_BASELINE)
			} else if job.Preset.Video.Profile == "main" {
				cc.SetProfile(gmf.FF_PROFILE_H264_MAIN)
			} else if job.Preset.Video.Profile == "high" {
				cc.SetProfile(gmf.FF_PROFILE_H264_HIGH)
			}
		}

		width, height, err := getResolution(job, ist.CodecCtx().Width(), ist.CodecCtx().Height())
		if err != nil {
			return 0, 0, err
		}

		bitrate, err := strconv.Atoi(job.Preset.Video.Bitrate)
		if err != nil {
			return 0, 0, err
		}

		cc.SetBitRate(bitrate)
		cc.SetDimension(width, height)
		cc.SetPixFmt(ist.CodecCtx().PixFmt())
	}

	if err := cc.Open(nil); err != nil {
		return 0, 0, err
	}

	ost.SetCodecCtx(cc)

	return ist.Index(), ost.Index(), nil
}

func getResolution(job types.Job, inputWidth int, inputHeight int) (int, int, error) {
	width, err := strconv.Atoi(job.Preset.Video.Width)
	if err != nil {
		return 0, 0, err
	}
	height, err := strconv.Atoi(job.Preset.Video.Height)
	if err != nil {
		return 0, 0, err
	}

	return width, height, nil
}

// FFMPEGEncode function is responsible for encoding the file
func FFMPEGEncode(jobID string) error {
	gmf.LogSetLevel(gmf.AV_LOG_FATAL)
	dbInstance, _ := db.GetDatabase()
	job, _ := dbInstance.RetrieveJob(jobID)
	srcFileName := job.LocalSource
	dstFileName := job.LocalDestination
	stMap := make(map[int]int, 0)
	var lastDelta int64

	inputCtx, err := gmf.NewInputCtx(srcFileName)
	if err != nil {
		return err
	}
	defer inputCtx.CloseInputAndRelease()

	outputCtx, err := gmf.NewOutputCtx(dstFileName)
	if err != nil {
		return err
	}
	defer outputCtx.CloseOutputAndRelease()

	job.Status = types.JobEncoding
	job.Details = "0%"
	dbInstance.UpdateJob(job.ID, job)
{'fraction_non_alphanumeric': 0.141572666339709, 'fraction_numerical': 0.013078306359326467, 'mean_word_length': 3.8983186549239393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2319366', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2459, 'n_words': 491}