Jump to content

Cutting to video length


arrbee99

Recommended Posts

arrbee99

I have quite a few mkvs that have audio tracks that are longer than video tracks. Would anyone know some ffmpeg that'll copy (not transcode) such a video with all audio and subtitle tracks but end it when it gets to the end of the video track. Thanks

I tried this -

REM Testing for loop
FOR %%m in (*.mkv) do (
REM Cut to video length
ffmpeg -i "%%m" -ss -map 0:a? -map 0:s? -map 0:v -c:v copy -c:a copy -c:s copy -shortest "%%~nm_length.mkv"
echo ====================================================
)

but it doesn't work, so...

Link to comment
Share on other sites

arrbee99

Getting rid of the -ss makes it run, but the output file is the same length as the input file and not shorter.

Link to comment
Share on other sites

  • 4 weeks later...
rbjtech

just seen this

always put -ss and -t BEFORE the -i (input)

I believe -shortest is only going to work for a encode - as you are using copy it may reject the filter.

Also - why are the audio/video different lengths - sounds like you have sync issues and you should probably fix that (re-time one or the other) rather than just chop it off ! 🤣

Link to comment
Share on other sites

arrbee99

Tried moving the -ss, didn't do anything, as in no processing. Also tried getting rid of -c:v copy, all that did was compress the file but leave it the same length, so lots of nothing left on the end as before.

Everything seems in sync, they're just episodes from a BR rip that have 5 minutes  longer audio (of nothing) than video.

Link to comment
Share on other sites

arrbee99

Would you happen to know if there's a way to get the duration of a video using ffprobe. Or rather the duration of the actual video track within a video ?

Link to comment
Share on other sites

rbjtech

I didn't look into it much tbh, but if it's a fixed 5 minutes over - then it should be relatively easy.

You need to demux the streams , then on the Audio stream, play with the duration (as above from Justin) minus 300 seconds to get the correct audio length.    Maybe take a look at -sseof -t 300 - which starts the count from the end of the file, not the beginning.

As you know ffmpeg is insanely powerful - it's just a matter of persevering with the arguments until it does what you want it to !

Once you have the correct audio length - then remux.

You can probably do it all in one argument - depends how dedicated you are !

Link to comment
Share on other sites

arrbee99

Nothing is ever simple.

I think its confined to merely the first series out of 10...

I'll probably just get the length of the video stream for each affected episode using MediaInfo then chop the end off using ffmpeg, which I can do already. Don't need to demux anything then.

They're all different times btw.

Nah, not dedicated, get discouraged quickly, limited attention span. And those are my good points...

  • Haha 1
Link to comment
Share on other sites

TMCsw
On 8/30/2022 at 6:29 PM, arrbee99 said:

Would you happen to know if there's a way to get the duration of a video using ffprobe. Or rather the duration of the actual video track within a video ?

Something like this

ffprobe -hide_banner -v error -select_streams v:0 -show_entries "stream=width,height,duration" -of csv=nokey=1:p=0 "%%m"

 

  • Like 1
Link to comment
Share on other sites

justinrh

ffmpeg is powerful, but the syntax is hard to learn.  The documentation is very difficult to digest.  I understand your frustration with it.

Link to comment
Share on other sites

arrbee99
3 hours ago, TMCsw said:

Something like this

ffprobe -hide_banner -v error -select_streams v:0 -show_entries "stream=width,height,duration" -of csv=nokey=1:p=0 "%%m"

 

as in this ?

REM Testing for loop
FOR %%m in (*.mkv) do (
REM Cut to video length
ffprobe -hide_banner -v error -select_streams v:0 -show_entries "stream=width,height,duration" -of csv=nokey=1:p=0 "%%m"
echo ====================================================
)

It just flashes up something in a box, then its gone again.

Link to comment
Share on other sites

rbjtech
@echo off
REM Testing for loop
FOR /F "delims=" %%m in ('dir /a-s /b /s "m:\Films\*.mkv"') do (

REM Cut to video length
	ffprobe -hide_banner -v error -select_streams v:1 -show_entries "format=duration" -of csv=nokey=1:p=0 "%%m"
	)
pause

change for mkv location and location for ffprobe - this will scan all sub folders as well

duration is in seconds

in the for loop, you need to despecify delims - otherwise it will use the space in filenames as a delimiter.

ffprobe will not show a duration from the stream specifier, it's in format.  

Link to comment
Share on other sites

arrbee99

The good news is that it scans. The bad news is it seems to give the length of the episode, not the length of the actual video stream.

In this - https://trac.ffmpeg.org/wiki/FFprobeTips#Duration

it says 'Note: Not all formats, such as Matroska and WebM, store duration at the stream level resulting in duration=N/A.' Don't know if thats relevant.

Link to comment
Share on other sites

rbjtech
49 minutes ago, arrbee99 said:

The good news is that it scans. The bad news is it seems to give the length of the episode, not the length of the actual video stream.

In this - https://trac.ffmpeg.org/wiki/FFprobeTips#Duration

it says 'Note: Not all formats, such as Matroska and WebM, store duration at the stream level resulting in duration=N/A.' Don't know if thats relevant.

That's exactly why I used format=duration ;) - as the streams all came back with a n/a if I just used stream=duration. 

Not sure I see the problem ?  if you want the length of the audio stream, add -select_streams a (or a:1 etc) - just build on the syntax, you can use functions more than once on the same line - then you'll get all the info you need to use for further processing.

Edited by rbjtech
Link to comment
Share on other sites

I think I understand what you are trying to do now also I didn't realize your getting the length from mediainfo. It's strange (to Me) that it gets that it would get ripped like that and I'm not sure where mediainfo is pulling the info from.

So give his a try:

  1. Download the appropriate CLI (Command Line Interface)  version of MediaInfo for your computer

  2. unzip it to it’s own directory

  3. save the code below to a batch file

  4. edit the 2 set lines at the top to suit

  5. run batch file

  6. if the time(s) indicated after the -to are correct remove the “echo” before “ffmpeg”

  7. run it again

  8. have a beer or ...
@echo off

:: Set file specification
set FileSpec="m:\Films\*.mkv"

:: Set location of MediaInfo CLI executable (directory where mediainfo.exe CLI version is located)
set MICLI="C:\bin\mediainfo"

SETLOCAL
SETLOCAL ENABLEEXTENSIONS 
SETLOCAL ENABLEDELAYEDEXPANSION

if not exist "%MICLI%\mediainfo.exe" goto :PNF
if not exist "%FileSpec%" goto :FNF

FOR /F "delims=" %%m in ('dir /a-s /b /s "%FileSpec%"') do ( 
  "%MICLI%\mediainfo" --Inform="Video;%%Duration/String3%%" "%%m" > %TEMP%\Duration.tmp
  call :runff "%%m"
  echo ====================================================
)
goto :cleanup
:runff
  set /p Duration= < %TEMP%\Duration.tmp
  echo ffmpeg -y -hide_banner -v info -i %1 -to %Duration% -map 0:a? -map 0:s? -map 0:v -c copy "%~dpn1_length.mkv"
goto :eof

:cleanup
  if exist %TEMP%\Duration.tmp DEL %TEMP%\Duration.tmp
  Pause
  exit /b
:FNF
  echo file not found %FileSpec%
  pause
  exit /b
:PNF
  echo mediainfo.exe not found in directory %MICLI%
  pause 

As you can likely see batch scripting has manny linits but there are usually ways to work around them.

Good luck!

Link to comment
Share on other sites

arrbee99

Can't even get it to run -

:: Set file specification
set FileSpec="Y:\Ripping\Post Handbrake\Hawaii Five-0 Season 1\Hawaii Five-0 Season 1 Disc 1\*.mkv"

:: Set location of MediaInfo CLI executable (directory where mediainfo.exe CLI version is located)
set MICLI="C:\Program Files\MediaInfo"

Do need the quotes ?

Link to comment
Share on other sites

Did you do the first 2 steps? It looks like you are trying to run the GUI version as a CLI , the CLI version needs to be in a separate directory from the regular GUI version in C:\Program Files\. They are very different!

Link to comment
Share on other sites

arrbee99

I did, but couldn't get it to run either so changed to the above. Originally I downloaded it and unzipped it to

C:\Users\schoo\Downloads\MediaInfo_CLI_22.06_Windows_x64

and had

:: Set file specification
set FileSpec="Y:\Ripping\Post Handbrake\Hawaii Five-0 Season 1\Hawaii Five-0 Season 1 Disc 1\*.mkv"

:: Set location of MediaInfo CLI executable (directory where mediainfo.exe CLI version is located)
set MICLI="C:\Users\schoo\Downloads\MediaInfo_CLI_22.06_Windows_x64"

and no joy.

 

Link to comment
Share on other sites

arrbee99

Just says -

The system cannot find the path specified.
The system cannot find the file specified.

Press any key to continue...

Link to comment
Share on other sites

Change the first line to:

@echo on

and cut-n-past it back (mouse highlight text then "right-click" then past in message)

Link to comment
Share on other sites

arrbee99


Y:\>set FileSpec="Y:\Ripping\Post Handbrake\Hawaii Five-0 Season 1\Hawaii Five-0 Season 1 Disc 1\*.mkv"

Y:\>set MICLI="C:\Users\schoo\Downloads\MediaInfo_CLI_22.06_Windows_x64"

Y:\>SETLOCAL

Y:\>SETLOCAL ENABLEEXTENSIONS

Y:\>SETLOCAL ENABLEDELAYEDEXPANSION

Y:\>if not exist ""C:\Users\schoo\Downloads\MediaInfo_CLI_22.06_Windows_x64"\mediainfo.exe" goto :PNF

Y:\>if not exist ""Y:\Ripping\Post Handbrake\Hawaii Five-0 Season 1\Hawaii Five-0 Season 1 Disc 1\*.mkv"" goto :FNF
The system cannot find the path specified.

Y:\>FOR /F "delims=" %m in ('dir /a-s /b /s ""Y:\Ripping\Post Handbrake\Hawaii Five-0 Season 1\Hawaii Five-0 Season 1 Disc 1\*.mkv""') do (
""C:\Users\schoo\Downloads\MediaInfo_CLI_22.06_Windows_x64"\mediainfo" --Inform="Video;%Duration/String3%" "%m"  1>C:\Users\schoo\AppData\Local\Temp\Duration.tmp
 call :runff "%m"
 echo ====================================================
)
The system cannot find the file specified.

Y:\>goto :cleanup

Y:\>if exist C:\Users\schoo\AppData\Local\Temp\Duration.tmp DEL C:\Users\schoo\AppData\Local\Temp\Duration.tmp

Y:\>Pause
Press any key to continue . . .

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...