15

My goal is simple , I have several webm files need to be concated, but first I need to determine their durations.

It seems webm file are played as streams, so there is no way to tell the length of each file.

I have been using ffprobe to do the job ,but the duration returned is N/A.The command I use was:

ffprobe -i input.file -show_format | grep duration

thanks.

The complete output of ffprobe list below:

 ffprobe version 2.6.2 Copyright (c) 2007-2015 the FFmpeg developers
  built with Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/2.6.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc --enable-libxvid --enable-libtheora --enable-libvorbis --enable-libvpx --enable-vda
  libavutil      54. 20.100 / 54. 20.100
  libavcodec     56. 26.100 / 56. 26.100
  libavformat    56. 25.101 / 56. 25.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 11.102 /  5. 11.102
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, matroska,webm, from '231':
  Metadata:
    encoder         : GStreamer matroskamux version 1.5.91
    creation_time   : 2015-12-05 07:59:29
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0(eng): Video: vp8, yuv420p, 640x480, SAR 1:1 DAR 4:3, 14.99 fps, 14.99 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      title           : Video
    Stream #0:1(eng): Audio: vorbis, 48000 Hz, stereo, fltp (default)
    Metadata:
      title           : Audio
duration=N/A
Lopakhin
  • 269
  • 1
  • 3
  • 16
  • Can you provide the complete output of the ffprobe command? This is indeed how it's supposed to be done, and it works on my collection of webm files. I'm wondering if your files are somehow using the .webm extension, but actually not webm files but something else (e.g. ivf). – Ronald S. Bultje Dec 06 '15 at 13:56
  • thanks, I pasted the output in the text, and I make sure it is encoded as webm, for those files can be viewed in stream fashion in chrome. – Lopakhin Dec 06 '15 at 14:02
  • 2
    It sounds like they were created as streamable webm files using matroskamux in GStreamer. These files indeed have no duration element and thus their duration is not known to the application. You could rewrite the file header based on contents (similar to avi reindexing), but ffmpeg does not do that right now. – Ronald S. Bultje Dec 07 '15 at 01:12
  • what you said is true , thanks – Lopakhin Dec 07 '15 at 07:56
  • @RonaldS.Bultje It does that rigth now, see my answer. – rnrneverdies Oct 21 '16 at 13:58

5 Answers5

31

I faced the same issue with some files that does not contain the Duration nor Bitrate on it and found the following solution:

1- Repackage the files with: (Note that this won't transcode the files, just will copy them)

ffmpeg -i source.webm -vcodec copy -acodec copy new_source.webm

2- Take the duration from the new copied file:

ffprobe new_source.webm | grep Duration
rnrneverdies
  • 15,243
  • 9
  • 65
  • 95
  • Thank you very much, I have been plagued by this problem for a few days. Your answer saved me – Black-Hole May 21 '19 at 06:20
  • In passing, seems the [MediaRecorder API](https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder) only produces raw / headerless audio files, but maybe I missed a way (from the client) to convert the stream into a proper file / add the headers. – Fabien Snauwaert Sep 02 '20 at 12:53
0

Container Duration

Print total seconds:

ffprobe -v error -show_entries format=duration \ 
-of default=noprint_wrappers=1:nokey=1 input.webm  

Result example: 221.333000.
Adding the -sexagesimal option, the result will show HH::MM::SS time unit format.
Result example: 0:03:41.333000.

Reference: https://superuser.com/a/945604/614421

Community
  • 1
  • 1
Daniel
  • 2,195
  • 3
  • 14
  • 24
  • 3
    This unfortunately does not work for raw webm files. – Gee Bee Feb 22 '18 at 22:21
  • 1
    webm files without headers will show neither duration, nor bitrate. Unfortunately you have to "convert" them - even converting them to webm works - then you will get headers and the expected output. – Gee Bee Feb 26 '18 at 17:34
  • This didn't work entirely for me, as there were several streams but only the longest one was reported... I was able to get it working by specifying what `stream_tags` to display. I posted an example as an answer. – rolandog Nov 09 '21 at 10:41
0

Run the command

ffprobe -v quiet -print_format json -show_format  -show_streams input.mp4

If you are looking for duration of each stream in webm container, the field name and location is different in the ffprobe output. In each stream you would see a field tags

"tags": {
                "language": "eng",
                "HANDLER_NAME": "ISO Media file produced by Google Inc. Created on: 12/09/2019.",
                "VENDOR_ID": "[0][0][0][0]",
                "ENCODER": "Lavc59.4.101 libvorbis",
                "DURATION": "00:03:14.703000000"
            }

You would have to convert this human readable time format into seconds. You can easily do with following utility function.

def convert_secs(text):
    if isinstance(text, float):
        num = str(text)
        nums = num.split('.')
    else:
        nums = text.split(':')
    if len(nums) == 2:
        st_sn = int(nums[0]) * 60 + float(nums[1])
        return st_sn
    elif len(nums) == 3:
        st_sn = int(nums[0]) * 3600 + int(nums[1]) * 60 + float(nums[2])
        return st_sn
    else:
        raise ValueError("Not correct time")
rusty
  • 652
  • 7
  • 21
0

The following solution is based on Daniel's version, but I modified it so I could inspect the individual stream lengths instead. This may help in the case where the duration is written in the metadata as TAG:DURATION.

ffprobe \
-v error \
-sexagesimal \
-show_entries stream=index,codec_name,start_time,duration:stream_tags=duration \
video.webm

Results in:

[STREAM]
index=0
codec_name=av1
start_time=0:00:00.007000
duration=N/A
TAG:DURATION=00:06:03.631000000
[/STREAM]
[STREAM]
index=1
codec_name=opus
start_time=0:00:-0.007000
duration=N/A
TAG:DURATION=00:06:04.001000000
[/STREAM]

This is useful because, as you can see, the streams report a different duration and you can identify problems in an easier manner.

rolandog
  • 159
  • 4
  • 9
-3

I ran into your question while trying to solve mine: Re-encoding vlc-created mpeg2 .ts file results in 20 second file

If you want to find the duration, use something like this:

ffmpeg -probesize 90M -analyzeduration 90M -i my_mpeg2_file.webm

I chose 90M since my file was just over 70mb, choose the appropriate size according to your file.


EDIT:

Not sure why it is not working for others, I have now tested with multiple .webm files, and am having success getting the duration for all of them. If it fails for you, can you please provide a link to the webm file so I can test? In my case, it worked for the following:

https://thepaciellogroup.github.io/AT-browser-tests/video/ElephantsDream.webm

http://dl3.webmfiles.org/big-buck-bunny_trailer.webm

insaner
  • 1,641
  • 16
  • 28
  • Downvoter, care to comment why? This solution works perfectly and requires no intermediate processing (like the other answer). – insaner Mar 28 '17 at 18:36
  • I didn't downvote you, but I tried your solution and it didn't work for me: $ ffmpeg -probesize 90M -analyzeduration 90M -i android_h264_VP8_a.webm ffmpeg version 3.4 Copyright (c) 2000-2017 the FFmpeg developers [...] At least one output file must be specified Tweaking the command line should help, but it appears to want an output file so still there is intermediate processing. – mportuesisf Dec 11 '17 at 18:40
  • @mportuesisf can you state your ffmpeg version? – insaner Dec 17 '17 at 06:06
  • 1
    This is old, but your answer is downvoted due to it not answering the question about webm files. Your answer talks about .ts files and doesn't work at all when used with webm. – ezwrighter Nov 30 '18 at 23:39
  • @ezwrighter thanks for the heads up. I will try to fix the answer when I have a chance – insaner Jan 04 '19 at 20:14
  • @mportuesisf, I have edited my response, after testing with some .webm files I found online. It seems to be working, not sure why it doesn't work for others. Can you provide me a link to a file on which this command didn't work? – insaner Jan 18 '19 at 20:34