LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-02-2020, 05:29 PM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up Bash script to build up a command


I know how to use getopts to get input to my bash script, and I know how to write a loop.


What I want to do is to prompt for a start and end points to cut from a video file, and then to build up an ffmpeg command to process the file (I know how to do that).


So it will prompt for

file name - just once - no problem here
then it should prompt for


Start

End


repeat this till it gets an E for Execute


Then I know how to write loop using a count on the number of start /end pairs read.


Quedtion how do I store the start/end pairs, in an array or a list, or some other way?



I'm just looking for some tips please.
 
Old 07-02-2020, 06:02 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Basically what ever is easiest for you. Can you build up the command as you prompt for start/end? Then if necessary just concatenate the strings together.

If so then the added complexity of using an array is not required.
 
2 members found this post helpful.
Old 07-02-2020, 06:42 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
The best suggestion, since you know how to do nearly all of it, is to start writing a script and post it.

See my blog, in my signature for tips.
 
Old 07-03-2020, 03:12 AM   #4
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Cheers all
 
Old 07-03-2020, 04:39 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
That is a script I would love to use myself!
Have you written it yet? Can you share it?
 
Old 07-03-2020, 06:37 AM   #6
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by ondoho View Post
That is a script I would love to use myself!
Have you written it yet? Can you share it?
In progress - and yes I will share it
 
Old 07-03-2020, 10:31 AM   #7
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by ondoho View Post
That is a script I would love to use myself!
Have you written it yet? Can you share it?
Here it is, a bit crude, but it works
Code:
#!/bin/bash
finished=false
while ! $finished; do
read -e -p "Movie To cut up: " FN
if [ ! -f "$FN" ]; then
    echo "File not found - try again"
    echo "=========================="
else
    finished=true
fi
done

echo "Now get on with the script"
unset T1
unset T2
finished=false
COUNT=0
while ! $finished; do
read -p "start cut - (0 to finish): " START
if [ "$START" -eq "0"  ]; then 
    break 
fi

read -p "end cut: "   END
((COUNT++))     ## iNCREMENT A COUNTER
echo $COUNT $START $END 
T1+="[0:v]trim=start=$START:end=$END,setpts=PTS-STARTPTS[pv$COUNT];[0:a]atrim=start=$START:end=$END,asetpts=PTS-STARTPTS[pa$COUNT];"
T2+="[pv$COUNT][pa$COUNT]"
done
echo "Now get on with the script again"
T2+="concat=n=$COUNT:v=1:a=1[VV][AA]"
echo $T1$T2
echo "/usr/bin/ffmpeg -y -i "$FN" -filter_complex "$T1""$T2" -map \"[VV]\" -map \"[AA]\" outx.mp4" > CutOutBits.txt
 /usr/bin/ffmpeg -y -i "$FN" -filter_complex "$T1""$T2" -map "[VV]" -map "[AA]" outx.mp4
I just need to prompt for an output filename
 
Old 07-03-2020, 11:10 AM   #8
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
I should add that the script does not do any validation other than the existance of the input file. It should check the start point is less than the end point, it should check it's not overwriting a file on output, and maybe it should let you input the start and end points in hh:mm:ss.s format, at the moment it expects seconds - I think.
 
Old 07-04-2020, 03:40 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
On modern ffmpeg versions I think one should use the '-ss' and '-t' options, along with '-c:a copy -c:v copy' to avoid re-encoding.
 
Old 07-04-2020, 05:21 AM   #10
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by ondoho View Post
On modern ffmpeg versions I think one should use the '-ss' and '-t' options, along with '-c:a copy -c:v copy' to avoid re-encoding.
I do that when I just want one clip, I'll try it doing multiple clips. And yes I know I could clip the pieces I want to keep in a nuber if ffmpeg runs and then joining them with another ffmpeg command, followed by deleting the clips.


I really want to do this with a single ffmpeg command.


And in some instances my input file is a .mts file (video recorded on a cheap chinese set top box) and I do want to re-encoe it to .mp4.


Cheers
 
Old 07-04-2020, 05:30 AM   #11
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by GPGAgent View Post
I really want to do this with a single ffmpeg command.
I understand.
But avoiding re-encoding is the more important thing here IMO. Snipping together bits from one video file should not require that.
I do not know whether your command does that or not; I suspect your "filter_complex" option does re-encode. Quick test: How long does it take (on a fairly recent consumer device), for a few minutes of video?
 
Old 07-04-2020, 06:10 AM   #12
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by ondoho View Post
I understand.
But avoiding re-encoding is the more important thing here IMO. Snipping together bits from one video file should not require that.
I do not know whether your command does that or not; I suspect your "filter_complex" option does re-encode. Quick test: How long does it take (on a fairly recent consumer device), for a few minutes of video?
I need to re-encode from .mts to .mp4, and yes it does in fact re-encode


I'll do a test of .mp4 to .mp4 and report back - cheers
 
Old 07-04-2020, 09:01 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I would try to use array[s] when I construct a command like this.
www.shellcheck.net will give you some additional hints.
 
Old 07-04-2020, 09:55 AM   #14
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by pan64 View Post
I would try to use array[s] when I construct a command like this.
www.shellcheck.net will give you some additional hints.
Cheers pan64, but using a variable is working just fine.
 
Old 07-04-2020, 10:18 AM   #15
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
My cutting script is working just fine, but for one final tweak, always the case innit!

I've added -s WxH so I can change the output size to suit where I'm going to be playing the video. I hold this in a variable called SIZE

This is the relevant bit of the bash script with the variable SIZE
Code:
SIZE="-s 512x288"
echo SIZE: $SIZE
echo "/usr/bin/ffmpeg -y -i ${FN} -filter_complex \"${T1}\"${T2} -map \"[VV]\" -map \"[AA]\" $SIZE ${OFN}" > CutOutBits.txt
/usr/bin/ffmpeg -y -i "${FN}" -filter_complex "${T1}""${T2}" -map "[VV]" -map "[AA]"  $SIZE "${OFN}"
exit
This is the problem - the - gets dropped from -s 512x288 on the ffmpeg command but you can see it's there when I echo it before executing the ffmpeg command
Terminal output:
Code:
charlie@charlie-machine:~/000TEST/Cutting$ ./CutOutBits.sh 
Movie To cut up: beach.mp4 
Output filename example.mp4: rtyu.mp4
start cut (h:mm:ss) - (e to finish) : 0:0:10
end cut (h:mm:ss): 0:0:50
1 10 50
start cut (h:mm:ss) - (e to finish) : e
Now get on with the script again
SIZE: -s 512x288
      ==========
    ffmpeg messages follow and removed
Unrecognized option 's 512x288'.
Error splitting the argument list: Option not found
charlie@charlie-machine:~/000TEST/Cutting$
And you can see in the script I save the command to a file which is this and you can see $SIZE has been expanded correctly i.e. -s 512x288
Code:
/usr/bin/ffmpeg -y -i beach.mp4 -filter_complex "[0:v]trim=start=10:end=50,setpts=PTS-STARTPTS[pv1];[0:a]atrim=start=10:end=50,asetpts=PTS-STARTPTS[pa1];"[pv1][pa1]concat=n=1:v=1:a=1[VV][AA] -map "[VV]" -map "[AA]" -s 512x288 rtyu.mp4
I can paste the above code into a terminal and works just fine, no reason why it shouldn't.
Whats causing the SIZE variable to expand incorrectly? How can i preserve the minus sign?


Breaking news - a workround - of sorts

I removed the -s from the variable so SIZE only has WxH, and the -s is in command line now -s $SIZE
Code:
SIZE="448x252"
/usr/bin/ffmpeg -y -i "${FN}" -filter_complex "${T1}""${T2}" -map "[VV]" -map "[AA]" -s $SIZE "${OFN}"
                                                                                     =========
exit

Last edited by GPGAgent; 07-04-2020 at 10:39 AM. Reason: update
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [SOLVED] bash script: can echo command, command works if I type, but command doesn't work in script ... why? hopeless_n00b Linux - Newbie 10 07-12-2018 05:57 AM
[SOLVED] Linux 4.4 seems to refuse to build IPv6 for the 64-bit build - 32-bit build works fine GameCodingNinja Linux From Scratch 2 02-07-2016 06:40 PM
Bash script - command works directly in command line but not in script raoulcousins Linux - Newbie 6 08-21-2013 07:43 PM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
LXer: Build 'em Right, Build 'em Strong, Build 'em Linux LXer Syndicated Linux News 0 10-01-2007 09:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration