LinuxQuestions.org
Review your favorite Linux distribution.
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 10-01-2020, 10:05 AM   #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
Script to cut adverts out of a video


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
Cheers folks
 
Old 10-01-2020, 02:04 PM   #2
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'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.
 
1 members found this post helpful.
Old 10-01-2020, 03:40 PM   #3
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 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
 
Old 10-02-2020, 01:10 AM   #4
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
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 View Post
Also it's a timed recording and I always add a few minutes to each end to account for it running late.
Sorry, but you lost me there.
 
Old 10-02-2020, 01:47 AM   #5
lvm_
Member
 
Registered: Jul 2020
Posts: 925

Rep: Reputation: 337Reputation: 337Reputation: 337Reputation: 337
You may use ffmpeg's trim filter to remove multiple segments in a single pass, check this link https://superuser.com/questions/6818...o-using-ffmpeg
 
1 members found this post helpful.
Old 10-02-2020, 03:30 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
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.

Last edited by GPGAgent; 10-02-2020 at 03:33 AM.
 
Old 10-02-2020, 05:32 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 lvm_ View Post
You may use ffmpeg's trim filter to remove multiple segments in a single pass, check this link https://superuser.com/questions/6818...o-using-ffmpeg
Nice one, I'll bear this in mind, some of the suggestions mirror my script so I'll be sticking with it for now, cheers
 
Old 10-02-2020, 11:40 AM   #8
petelq
Member
 
Registered: Aug 2008
Location: Yorkshire
Distribution: openSUSE(Leap and Tumbleweed) and a (not so) regularly changing third and fourth
Posts: 627

Rep: Reputation: Disabled
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.
 
Old 10-02-2020, 12:23 PM   #9
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
Thumbs up

Quote:
Originally Posted by petelq View Post
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:
Code:
#!/bin/bash
#                    file    preset  st  en   st      en      up to five cuts
# ./CutAndEncode.sh MAH.mts faster 41:14 53:53 57:42 1:09:17
# -preset  ultrafast superfast veryfast faster fast medium (default) slow veryslow
# 
#

../CropCutAndEncode.sh CBS_Justice-09232020-0348.mts veryfast 1:45 16:48 17:24 27:51 28:27 45:03 45:39 52:52
../CropCutAndEncode.sh CBS_Justice-09242020-0348.mts veryfast 3:00 15:51 16:30 26:11 26:50 37:00 37:39 53:58
../CropCutAndEncode.sh CBS_Justice-09252020-0348.mts veryfast 3:55 52:50
../CropCutAndEncode.sh CBS_Justice-09262020-0348.mts veryfast 2:34 51:54
../CropCutAndEncode.sh CBS_Justice-09262020-1059.mts veryfast 11:30 26:27 31:50 42:15 47:38 1:05:00
../CropCutAndEncode.sh CBS_Justice-09272020-1059.mts veryfast 13:58 29:09 33:01 42:34 45:55 57:16 1:00:36 1:05:00
../CropCutAndEncode.sh CBS_Justice-09292020-1059.mts veryfast 00:48 13:12 17:35 29:10 33:33 41:04 44:27 1:01:14
../CropCutAndEncode.sh CBS_Justice-09302020-0348.mts veryfast 03:11 52:36
../CropCutAndEncode.sh CBS_Justice-10012020-0348.mts veryfast 03:47 52:59
../CropCutAndEncode.sh CBS_Justice-10022020-0348.mts veryfast 03:41 52:50
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.
 
Old 10-02-2020, 05:54 PM   #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 petelq View Post
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
 
Old 10-02-2020, 06:53 PM   #11
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,982

Rep: Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625
Might as well mention comskip.
 
Old 10-03-2020, 02:54 AM   #12
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
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.
 
Old 10-03-2020, 03:22 AM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Gotta to be a candidate for machine learning ...
 
Old 10-03-2020, 05:10 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 ondoho View Post
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.
 
Old 10-03-2020, 08:31 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
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.

Cheers
Attached Thumbnails
Click image for larger version

Name:	Time.jpg
Views:	12
Size:	65.0 KB
ID:	34216   Click image for larger version

Name:	Time.v01.png
Views:	13
Size:	19.0 KB
ID:	34217   Click image for larger version

Name:	Time3.png
Views:	9
Size:	33.9 KB
ID:	34218  
 
  


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] using cut -d" " to cut out the string before, not after slowerogue Linux - Newbie 3 01-09-2015 05:37 AM
Squid proxy: block Flash adverts hamish Linux - Software 4 02-13-2012 07:42 AM
the adverts on LQ homepage ylawayjdp LQ Suggestions & Feedback 1 03-24-2005 03:29 PM
Designing Adverts software? HadesThunder Linux - Newbie 3 06-11-2004 04:40 PM
Best tv adverts acid_kewpie General 3 03-23-2003 09:19 AM

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

All times are GMT -5. The time now is 10:37 PM.

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