Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've written a script that lets me cut bits out of a video but I think it could be slicker, basically thus script takes a filename, number of cuts (I don't usit tho') Start and end of first bit to keep, start and end of second bit to keep and so on up to five bits to keep.
Firstly I'd like to not have limit of just up to five bits to keep, and secondly how could the script be improved, here's the script:
Code:
#!/bin/bash
# usage:
# CutAndEncode.sh Filename Cuts Start1 End1 Start2 End2 Start3 End3 ... Start5 End5
cnt=1
for P in "$@"
do
case $cnt in
1) FN=$P;;
2) CUTS=$P;;
3) ST=$P;;
4) EN=$P
echo "Cut $cnt of $FN start at $ST and end at $EN"
ffmpeg -ss $ST -to $EN -i "$FN" -vf "yadif" -aspect 4:3 -c:v libx265 -preset faster Tmp-$cnt.mp4;;
5) ST=$P;;
6) EN=$P
echo "Cut $cnt of $FN start at $ST and end at $EN"
ffmpeg -ss $ST -to $EN -i "$FN" -vf "yadif" -aspect 4:3 -c:v libx265 -preset faster Tmp-$cnt.mp4;;
7) ST=$P;;
8) EN=$P
echo "Cut $cnt of $FN start at $ST and end at $EN";;
9) ST=$P;;
10) EN=$P
echo "Cut $cnt of $FN start at $ST and end at $EN";;
11) ST=$P;;
12) EN=$P
echo "Cut $cnt of $FN start at $ST and end at $EN";;
13) ST=$P;;
14) EN=$P
echo "Cut $cnt of $FN start at $ST and end at $EN";;
esac
cnt=$((cnt + 1))
done
echo "Now join the cuts"
ls Tmp*.mp4 | perl -ne 'print "file $_"' | ffmpeg -protocol_whitelist "file,pipe" -safe 0 -y -f concat -i - -codec copy $FN.mp4
echo "Now delete the temp files"
rm Tmp*.mp4
I'd like to not have limit of just up to five bits to keep
I recommend writing a script that only cuts out one bit, and use that repeatedly.
Also don't transcode the streams, just use '-c:v copy -c:a copy'. That way you can losslessly operate on your videos forever.
I recommend writing a script that only cuts out one bit, and use that repeatedly.
Also don't transcode the streams, just use '-c:v copy -c:a copy'. That way you can losslessly operate on your videos forever.
Thanks, I want an mp4 file, the source is mts so I need to transcode it, and there is an advert in the middle, so I want to cut it out. Also it's a timed recording and I always add a few minutes to each end to account for it running late. So I need to cut a bit from the beginning, a bit in the middle (advert) and a bit off the end, in fact there could be many advert breaks to remove. Something like this:
Code:
|==preamble|===|advert|====|==post===|
|===| |====| these are the bits I want
Does that explain it?
Last edited by GPGAgent; 10-02-2020 at 03:22 AM.
Reason: Got the illustration wrong
Transcode first, do the (very simple) cutting next.
Or, do the cutting first and transcode the finished product last.
The point is that the cutting-and-pasting is very fast and a very simple command, there's no point writing a script around it.
Since you never know beforehand how many ad breaks there are and how long they are.
Quote:
Originally Posted by GPGAgent
Also it's a timed recording and I always add a few minutes to each end to account for it running late.
Transcode first, do the (very simple) cutting next.
Or, do the cutting first and transcode the finished product last.
The point is that the cutting-and-pasting is very fast and a very simple command, there's no point writing a script around it.
Since you never know beforehand how many ad breaks there are and how long they are.
Sorry, but you lost me there.
A timed recording - I set my video to record a program from broadcast tv, I always set the timer to start a couple of minutes before the scheduled start time and to stop about five minutes after the scheduled end time. If you don't do that you could miss the beginning or end.
And yes there can be many ad beaks, I cannot think how to detect these so I quickly play the video and note down the start and end times, these are used as inputs to the script. The script is written to handle up to five chops as it were.
As I'm going to transcode the video I may as well cut it up, transcode that bit, transcode all the bits and then join the bits up, joining is very quick as is just a copy basically.
I'm happy with the method, would like to recode the script to be more elegant and to handle any number of cuts.
Distribution: openSUSE(Leap and Tumbleweed) and a (not so) regularly changing third and fourth
Posts: 627
Rep:
I use avidemux to work through the file and cut out the bits you don't need. It's pretty quick and as you are going through the file to find the cut points anyway, you might as well cut as you find.
I use avidemux to work through the file and cut out the bits you don't need. It's pretty quick and as you are going through the file to find the cut points anyway, you might as well cut as you find.
If it was just the one video then I might do that, as it is it's quicker to zip through the video, noting the start and end points of the bits I want to keep and then running the cli script - this is the driver script:
Which calls the cropping/cutting/encoding/joining script, and the script moves the original and the processed video to a new folder. So if I need to stop this at anytime it's easy to just start again from where I left off.
So the original .mts files are about 1Gb and my processed videos are about 180Mb in size, quite a saving.
I use avidemux to work through the file and cut out the bits you don't need. It's pretty quick and as you are going through the file to find the cut points anyway, you might as well cut as you find.
I just tried avidemux, not too bad, does the job, nice interface, could see how to cut a chunk out and save it, couldn't see how to remove chunks tho', I need to spend more than five minutes trying it out.
Filters look good as do the transcoding options, does exactly what I want, but, and it's a big but, I couldn't see how to set it up to process ten different videos and then just leave it to run through them.
My script is simple to construct, takes about a minute or less to write each line in the Driver script, then I just leave it running till it's all completed, and I can get on with something else.
I'll play with it a bit more, ot may be useful to do some further video work like cropping which I cannot automate.
Cheers
I had a closer look at your script now and it does in fact not duplicate encoding, so I was wrong to criticize that.
A few things:
the way you're parsing cli arguments is clumsy, you should really look at 'getopts' (that's the bash builtin, 'help getopts') or 'getopt' (util-linux) and some examples/tutorials on the web.
all your start/end points could be just one command line option, e.g. a comma-separated list
tramsform that list into an array: array=( ${list//,/ } )
loop through that array and do your cuts
That way you could pass unlimited start/end points.
I had a closer look at your script now and it does in fact not duplicate encoding, so I was wrong to criticize that.
A few things:
the way you're parsing cli arguments is clumsy, you should really look at 'getopts' (that's the bash builtin, 'help getopts') or 'getopt' (util-linux) and some examples/tutorials on the web.
all your start/end points could be just one command line option, e.g. a comma-separated list
tramsform that list into an array: array=( ${list//,/ } )
loop through that array and do your cuts
That way you could pass unlimited start/end points.
Cheers, my original thought was to use an array, then loop through it. Also thought of using getopts.
My problem is bash syntax, try as I might it doesn't come to me without finding an example, I can code sql queries, Coldfusion, HTML, Fortran and so on with no problem, simple bash, fine, more complex bash and I get stuck. I know what to do, but doing it in style takes me ages because I have to look up the syntax all the time.
And you're absolutely right, my script is not the best, but it works how I want it to.
Now if I could detect the start and end of an advert this could be automated.... Any ideas?
I should add some programs change aspect ratio, adverts at 16:9 and the actual show is 4:3 - if I could detect that change then job done.
I'm going to finish this off now, script will do as it is but to show how simple this is., it took me about 30 seconds from copying the mts file into a folder, running mpv to find the start and end times, see images, all you need to do us to place the cursor in the time bar at the botton of the mpv window, it's hidden until you put the cursor there. Then just use the left/right cursor keys to fast rev/fwd and note down the relevant times. Have the DriverScript.sh script open in xed and you can just edit this with the times.
Save and run it job done, all in 30 seconds, if you have ten videos this will take a maximum of 4-5 mins.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.