2

So I'm trying to setup an easy way of starting videos with a bat file, and having that run Mediainfo first to get the length of the video so it can then stop vlc or whatever else when it's done playing.

Complete name                            : C:\Users\Tyler\Desktop\Psych s05e11.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 116 MiB
Duration                                 : 42 min 36 s
Overall bit rate                         : 382 kb/s
Writing application                      : Lavf55.13.102

That's the output from mediainfo I got in a txt file, I'm trying to just pull the 42 and the 36 from the duration bit and use it in another command. I should also add that these numbers have to be used separately. Thanks!

Edit: Thanks for replying everyone love the help; Here's what I'm trying to run now:

mediainfo.lnk --Language=raw --Output=General;%Duration% "C:\Users\Tyler\Desktop\Psych s05e11.mp4"

and the output is:

2556249

Now I need a way to take the first four digits and use them in a another command, somehow make 2556 a variable?

3 Answers3

1

If you need the duration, use e.g. this command:

mediainfo "--Output=General;%Duration%" YourFileName.ext

In a general way, when you think to some automation, prefer to use e.g.:

mediainfo -f --Language=raw  YourFileName.ext

and select the lines which better fits your need, avoid fields with "/String" because they are intended only for display (not for automation).

Jérôme, developer of MediaInfo.

Jérôme Martinez
  • 1,118
  • 5
  • 10
  • 1
    I'm running the command like so: mediainfo --Language=raw --Output=General;%Duration% "filename.ext" >> "outputfile.txt" and I'm only getting the duration like I want, thanks for that help! – Tyler Lancaster Oct 06 '16 at 17:38
  • Here's a weird problem I've run into; when I run the command out of a terminal like I've shown in the last comment I get the proper output, however using the exact same command in a batch file returns an output without any options, it runs as: mediainfo --Output=file:\\file.txt "filename.ext". Giving too much information! What could I do to get around that? – Tyler Lancaster Oct 06 '16 at 18:50
  • Just to tell you (As developper) you shoul never use the `%` as var definer in a CLI version of a soft. Better use another char like `$`. Anyway great soft !! – SachaDee Oct 06 '16 at 19:48
  • Not a problem of MediaInfo, this is a problem in your batch: % is used for variable, you forgot to escape it. mediainfo "--Output=General;%%Duration%%" %1 – Jérôme Martinez Oct 07 '16 at 06:16
  • You're right i forgot the "string", Modified My Answer, Thanks – SachaDee Oct 07 '16 at 12:06
0

Using FOR /F

@echo off
for /f "delims=" %%a in ('mediainfo "--Output=General;%%Duration%%" "C:\Users\Tyler\Desktop\Psych\s05e11.mp4"') do set $Duration=%%a

Echo %$Duration%

Using a temporary file

@echo off
mediainfo.exe --Language=raw --Output=General;%%Duration%% "C:\Users\Tyler\Desktop\Psych s05e11.mp4" >out.txt  
set /p $Duration= <out.txt
set $Duration=%$Duration:~0,4%

echo Result = %$Duration%
del out.txt

Another way using @Jérôme Martinez [raw -output] idea. Without temporary file, using findstr :

@echo off
for /f "tokens=2 delims=:" %%a in ('mediainfo -f --Language=raw  "C:\Users\Tyler\Desktop\Psych s05e11.mp4" ^| findstr /i "duration"') do (
    set $Duration=%%a
    goto:next
)

:next
set $Duration=%$Duration:~1,4%
echo %$Duration%
SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • Thanks for the help; Seems closer than anything I've got so far! However the echo output is: Result = ~0,4 – Tyler Lancaster Oct 06 '16 at 18:54
  • Figured out the path was just set up wrong, got that situated now the output is: Result = Alte – Tyler Lancaster Oct 06 '16 at 19:00
  • Ok and when you just execute `mediainfo.lnk --Language=raw --Output=General;^%Duration^% "C:\Users\Tyler\Desktop\Psych s05e11.mp4"` What is the exact output ? In all case you'll better work directly on the `.exe` and not on the .lnk – SachaDee Oct 06 '16 at 19:09
  • If I run it in a terminal I get the expected 2556249 but if I run that in a bat file the output is the huge list of stuff mediainfo would display by default, I'll try running it using the exe file instead – Tyler Lancaster Oct 06 '16 at 19:11
  • I've run it again using the exe file instead of a link, same output, long list of stuff I don't need. However if I do that command by itself it prints out just the Duration – Tyler Lancaster Oct 06 '16 at 19:22
  • Let me make a test – SachaDee Oct 06 '16 at 19:30
  • Looks as though %Duration% is being looked at like a variable rather than a command option. That's why the ^ right? They don't seem to be working.. anything else that might work? – Tyler Lancaster Oct 06 '16 at 19:31
  • I tried something like that myself, but don't understand batch enough to get anywhere near that! Thanks for that! Quick question is the ~0,4% the option to use only the first 4 digits? – Tyler Lancaster Oct 06 '16 at 20:09
  • check my new EDIT with the first solution, And put this Post as answered – SachaDee Oct 07 '16 at 12:12
0

Okay here's what I did finally, thanks for all the help!

C:\mediainfo\MediaInfo.exe --Language=raw --Output=General;%%Duration%% "C:\Users\Tyler\Desktop\Psych_s05e11.mp4" >out.txt  
set /p $Duration= <out.txt
set $Duration=%$Duration:~0,4%

echo Result = %$Duration%
del out.txt
pause

and the output is:

C:\mediainfo\MediaInfo.exe --Language=raw --Output=General;%Duration%    "C:\Users\Tyler\Desktop\Psych_s05e11.mp4" >out.txt 
set /p $Duration=  <out.txt 
set $Duration=2556 
echo Result = 2556 
Result = 2556
del out.txt 
pause
Press any key to continue . . . 

took @echo off outta there so you could see it all