LinuxQuestions.org
Review your favorite Linux distribution.
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 12-12-2018, 05:06 AM   #1
GPGAgent
Member
 
Registered: Oct 2018
Posts: 234

Rep: Reputation: Disabled
How to cut the last 1 min of a movie?


Cut the last one minute of a movie? Sound simple enough and I've almost got it but my bash skills let me down on the simplest of things!

Step 1 get movie length - many ways here's one

Code:
jonk@XEON4 ~/CK $ ffprobe -i CK.mp4 -show_format -v quiet | sed -n 's/duration=//p'
301.040000
So that's a length of 301 seconds
Step 2 subtract 1 minute
Code:
jonk@XEON4 ~/CK $ echo $(( 301 - 60))
241
So that's a start point of 241 seconds, my -ss parameter for ffmpeg.

Step three combine all this in a script, I tried combining the first two steps

Code:
jonk@XEON4 ~/CK $ echo $(( $(ffprobe -i CK.mp4 -show_format -v quiet | sed -n 's/duration=//p') - 60))
bash: 301.040000 - 60: syntax error: invalid arithmetic operator (error token is ".040000 - 60")
jonk@XEON4 ~/CK $
So my question is how to combine a complex command in an arithmetic bash statement?

Or maybe there is another way to use ffmpeg to cut the last minute of a movie.

BTW I intend to loop through a folder of movies to cut the last minute from all of them which is why I want to parameterise this operation.

TIA
 
Old 12-12-2018, 05:47 AM   #2
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 892

Rep: Reputation: 288Reputation: 288Reputation: 288
I won't debate about your ffprobe methodology because I've never used it so far. So I'll limit myself to answering you on a pure bash/shell point of view:

bash does not support floating-point arithmetic. You need to use an external utility like bc.
So try the following instead:
Code:
echo "scale=2;$(ffprobe -i CK.mp4 -show_format -v quiet | sed -n 's/duration=//p') - 60" | bc

Last edited by l0f4r0; 12-12-2018 at 05:54 AM.
 
Old 12-12-2018, 05:54 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,576

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
there are two ways in general: remove the decimal part (for example with sed):
sed -n '/duration/s/duration=//;s/\.[0-9][0-9]*//p'
or use bc.
 
Old 12-12-2018, 01:51 PM   #4
GPGAgent
Member
 
Registered: Oct 2018
Posts: 234

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by l0f4r0 View Post
I won't debate about your ffprobe methodology because I've never used it so far. So I'll limit myself to answering you on a pure bash/shell point of view:

bash does not support floating-point arithmetic. You need to use an external utility like bc.
So try the following instead:
Code:
echo "scale=2;$(ffprobe -i CK.mp4 -show_format -v quiet | sed -n 's/duration=//p') - 60" | bc
That works fine, thanks
 
Old 12-12-2018, 01:54 PM   #5
GPGAgent
Member
 
Registered: Oct 2018
Posts: 234

Original Poster
Rep: Reputation: Disabled
Okay, thanks folks, I should have spotted that bash only handles integers, so I have my script working that cuts the last x minutes from a set of movies.

I use ffmpeg -ss to skip to x mins from the end then just copy that chunk of movie, very simple, but is there a better way to chop out the last x mins from a set of movies?

Last edited by GPGAgent; 12-12-2018 at 02:15 PM. Reason: spelling
 
Old 12-12-2018, 03:14 PM   #6
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 2,598

Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
Code:
#! /usr/bin/env bash

#Loop through all file in directory of videos
for i in *; do

    #Get duration of video
    duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$i")
    
    #Drop decimals, subtract 60 from duration
    dur=$((${duration%.*} - 60))
    
    #Echo ffmpeg command line
    echo "ffmpeg -t "$dur" -i "$i" -c:a copy -c:v copy NEW_"$i""
    
done
 
Old 12-12-2018, 06:08 PM   #7
GPGAgent
Member
 
Registered: Oct 2018
Posts: 234

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
Code:
#! /usr/bin/env bash

#Loop through all file in directory of videos
for i in *; do

    #Get duration of video
    duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$i")
    
    #Drop decimals, subtract 60 from duration
    dur=$((${duration%.*} - 60))
    
    #Echo ffmpeg command line
    echo "ffmpeg -t "$dur" -i "$i" -c:a copy -c:v copy NEW_"$i""
    
done
Basically that's what I've done

Code:
#!/bin/bash
renice 19 -p $$

bold=$(tput bold)
norm=$(tput sgr0)

default="*.mp4"
read -p "Movie Filename and Extension to Process [${bold}$default${norm}|*A|A*]: " FileNameAndExt
FileNameAndExt=${FileNameAndExt:-$default}

default=600
read -e -p "Get last part of movie  [${bold}$default${norm}] seconds from end: "  STARTAT
STARTAT=${STARTAT:-$default}        # -ss $STARTAT

echo "cutting ${FileNameAndExt} "

for FF in $FileNameAndExt;

    do ((COUNTER++)); 
            ss=$(echo "scale=2;$(ffprobe -i ${FF} -show_format -v quiet | sed -n 's/duration=//p') - $STARTAT" | bc)
      ffmpeg -i  ${FF} -ss ${ss} -c:v copy -c:a copy  "Last.${FF}"
    done

exit 0
Cheers
 
Old 12-16-2018, 10:31 AM   #8
GPGAgent
Member
 
Registered: Oct 2018
Posts: 234

Original Poster
Rep: Reputation: Disabled
just posted this:

https://www.linuxquestions.org/quest...in-4175644387/

A result of comments back from this thread.

Cheers all
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Get first day of last month and last day of last month in bash xowl Linux - Software 18 02-09-2017 10:49 AM
[SOLVED] From 2 days ago, it takes 10 min.s to boot my HP dv9700 laptop. Prior two yr.s, 2 min PTrenholme Linux - Hardware 6 12-28-2010 12:40 PM
help with cut command using find. Cut last 8 characters leaving the rest ncsuapex Programming 4 09-16-2009 09:55 PM
3.8 Gb Suse 10.1 slow install in 1h30 min. Mandriva 2006 was less than 40 min Emmanuel_uk Linux - Distributions 2 06-15-2006 10:27 AM

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

All times are GMT -5. The time now is 03:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration