You are not logged in.

#1 2013-12-01 02:55:33

z1lt0id
Member
Registered: 2012-09-20
Posts: 167

[SOLVED] How to figure out crop and resolution with bash scripting

I'm trying to create a bash script where I choose a video file (.ts stream) and I want to choose the crop and I want the bash script to automatically add these parameters and create the appropriate resolution.

For example.  I have a 720x576 interlaced video file and I want to crop 2 pixels left, right, top and bottom which then encodes to x264 piped from ffmpeg.

Here is what I have.

ffmpeg  -v -10 -analyzeduration 100M -probesize 100M -i "/mnt/snake-pliskin/video-edits/01.ts" -an -sn -r 25000/1000 -threads 8 -vf yadif=0:1,crop=716:572:2:2,scale=716:402 -vsync 0 -r 25 -pix_fmt yuv420p -sws_flags spline -f rawvideo - | x264 - --preset slow --crf 19 --profile high --level 3.1 --sync-lookahead 15 --threads 12 --colormatrix undef --fps 25000/1000 --input-res 714x402 -o bb.264

For the purpose I've given the exact paramaters in the crop= section instead of putting variables to show you how the command line is.

Does anyone the mathematics behind working this out.  Unfortunately I have no idea where to start.

Last edited by z1lt0id (2013-12-02 20:50:36)

Offline

#2 2013-12-01 14:18:28

barto
Member
From: Budapest, Hungary
Registered: 2009-10-22
Posts: 88

Re: [SOLVED] How to figure out crop and resolution with bash scripting

I’m not sure I understand you, but ffprobe (part of the ffmpeg package) can be used to retrieve info about a particular media file.
For example

$ eval $(ffprobe -loglevel quiet -show_streams video_720x576.avi | grep -E "width|height")

will put width and height of the video into the $width and $height variables respectively. These variables can be further processed by bash and used by ffmpeg.


“First principle, Clarice. Simplicity” – Dr. Hannibal Lecter

Offline

#3 2013-12-01 18:51:56

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,838
Website

Re: [SOLVED] How to figure out crop and resolution with bash scripting

I did this for myself a few years back:

mplayer `mplayer -ao null -ss $1 -frames 100 -vf cropdetect -vo null "$2" | awk -F '[()]' '{print $2}' | uniq | grep -Ev 'End of file' | tail -2` "$2"

where $1 is number of seconds where to start looking for borders and $2 is the actual clip.

Last edited by Rasi (2013-12-01 18:53:33)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#4 2013-12-02 00:47:56

DrZaius
Member
Registered: 2008-01-02
Posts: 165

Re: [SOLVED] How to figure out crop and resolution with bash scripting

z1lt0id wrote:

I have a 720x576 interlaced video file and I want to crop 2 pixels left, right, top and bottom which then encodes to x264 piped from ffmpeg.

You can use the iw (input width) and ih (input height) options in the crop video filter:

-vf crop=iw-4:ih-4

Using an input size of 720x576, this example will result in an output size of 716x572. Default behavior is to center the cropping (which is what I think you want).


Damned dirty ape.

Offline

#5 2013-12-02 00:53:00

z1lt0id
Member
Registered: 2012-09-20
Posts: 167

Re: [SOLVED] How to figure out crop and resolution with bash scripting

I ended up doing the following, my bash scripting isn't that great.  Using this as a project to learn.

Cropping Variable = 3 is a menu option where you can choose a custom crop.

echo -e " "
if [ "$cropping_variable" = "3" ]; then
        res_a=$(mediainfo --Inform="Video;%Width%" $input_variable.ts)
        res_b=$(mediainfo --Inform="Video;%Height%" $input_variable.ts)

                if [ "$res_a" -lt "1440" ]; then
                        wt=$(($res_a-left_variable-right_variable))
                        ht=$(($res_b-top_variable-bottom_variable))
                        wc=$left_variable
                        hc=$top_variable
                        ws=$wt

                        height=$(bc <<< "scale=1; $wt / 1.78")
                        wh=$(python -c "import math; print(math.ceil($height))")
                fi

                if [ "$res_a" = "1440" ]; then
                        wt=$(($res_a-left_variable-right_variable))
                        ht=$(($res_b-top_variable-bottom_variable))
                        wc=$left_variable
                        hc=$top_variable
                        ws="720"
                        wh="404"
                fi
fi

inres=$ws"x"$wh


clear

echo -e "\e[90m ----------------"
echo -e "\e[91m Extracting Audio"
echo -e "\e[90m ----------------"
echo "" 

ffmpeg -i $input_variable.ts -acodec pcm_s16le -ac 2 $input_variable.wav
echo
echo -e "\e[90m --------------"
echo -e "\e[91m Encoding Video"
echo -e "\e[90m --------------"
echo ""

if [ "$ws" -lt "720" ]; then
        ffmpeg -v 10 -analyzeduration 100M -probesize 100M -i $input_variable.ts -an -sn -r 25000/1000 -threads 8 -vf kerndeint,crop=$wt:$ht:$wc:$hc,scale=$ws:$wh -vsync 0 -r 25 -pix_fmt yuv420p -sws_flags lanczos -f rawvideo - | x264 - --preset slow --crf $crf_variable --profile high --level 3.1 --sync-lookahead 15 --threads 12 --fps 25000/1000 --input-res $inres -o $input_variable.h264

fi

if [ "$ws" = "720" ]; then
        ffmpeg -v 10 -analyzeduration 100M -probesize 100M -i $input_variable.ts -an -sn -r 25000/1000 -threads 8 -vf kerndeint,crop=$wt:$ht:$wc:$hc,scale=$ws:$wh -vsync 0 -r 25 -pix_fmt yuv420p -sws_flags lanczos -f rawvideo - | x264 - --preset slow --crf $crf_variable --profile high --level 3.1 --sync-lookahead 15 --threads 12 --colormatrix bt709 --fps 25000/1000 --input-res $inres -o $input_variable.h264
fi

Offline

#6 2013-12-02 11:00:03

Darksoul71
Member
Registered: 2010-04-12
Posts: 319

Re: [SOLVED] How to figure out crop and resolution with bash scripting

Personally I found the autocrop of mplayer always being "off" a few pixels which results in cropping more pixels away then neccessary. I hacked together a Python script for autocropping things with ffmpeg and GraphicsMagic. I do not feel like reposting everything. If you are interested then head over to doom9:
http://forum.doom9.org/showthread.php?t=168506

This script performs equally well as the handbrake autocropping mechanism !

Edit: Sorry...my bad...I misread your initial posting...you are rather looking into entering the cropping values manually and let your script do the correct resizing afterwards, right ?

Last edited by Darksoul71 (2013-12-02 13:10:50)


My archlinux x86_64 host:
AMD E350 (2x1.6GHz) / 8GB DDR3 RAM / GeForce 9500GT (passive) / Arch running from 16GB USB Stick

Offline

#7 2013-12-02 20:50:16

z1lt0id
Member
Registered: 2012-09-20
Posts: 167

Re: [SOLVED] How to figure out crop and resolution with bash scripting

It's okay Darksoul71 I figured out how to do it.  I'll put this post as solved.

Offline

Board footer

Powered by FluxBB