-2

I use

call(['avconv', '-i', 'video.mp4', '-vsync', '1','-r', '1','-an','-y','%5d.jpg'])

in Python. It works, but it goes through the videofile in realtime. How to speed this up, so getting 60 pictures all in all, each second of the video file does not take 1 minute but less.

johntellsall
  • 11,853
  • 3
  • 37
  • 32
  • 1
    it doesn't go through it in realtime... It goes as fast as it can. Writing lots of files to the disk is time consuming. – JBernardo Aug 16 '14 at 00:09
  • Why python tag? You are just calling an external application. – crs Aug 16 '14 at 00:17
  • @JBernardo 60 jpg is not lots of files. It just needs to skip each (fps * 60) frames and save the file. – Timo Theobald Aug 16 '14 at 00:17
  • @crs Cause I want a solution in python – Timo Theobald Aug 16 '14 at 00:17
  • How should this solution look like? Python does not do anything but call your external application and therefore there is not much space for performance improvements from the python side. However you might want to take a look at the avconv documentation available at https://libav.org/avconv.html and change your parameters in order to increase the performance. – crs Aug 16 '14 at 00:29
  • 1
    No matter how many pictures you want, all the video must be decoded in order to do that. It is called temporal compression – JBernardo Aug 16 '14 at 00:29
  • 1
    @crs is right. The Python tag should be removed. All the python code is done... you just want to change your ffmpeg command line. BTW ffmpeg should be superior than avconv – JBernardo Aug 16 '14 at 00:30
  • @TimoTheobald I'm not sure why you're under the impression that this is some sort of lightweight operation. Decoding MPEG-4 is a **heavy** operation, and creating then writing 60 JPEG files per second isn't exactly lightweight either. If you think you can do better, why don't you submit a patch for FFmpeg/avconv. Otherwise, you're going to have to change your parameters if you expect anything to change. Why don't you buy a faster machine? – Brad Aug 16 '14 at 15:09
  • @JBernardo according to ffmpeg, avconv is ffmpeg's successor. – Timo Theobald Aug 16 '14 at 19:10
  • @Brad You are wrong. It is not about creating 60 frames in one second. It is about extracting 1 frame each video time second. – Timo Theobald Aug 16 '14 at 19:11
  • 1
    @TimoTheobald No, thats avconv on Ubuntu only. They show this message because of some internal conflicts. Avconv is a fork with much less functionality. http://ffmpeg.org/ – JBernardo Aug 16 '14 at 23:01
  • @TimoTheobald Make it clearer in your question that you only want one frame extracted per second of video. And, my comment still stands... that video has to be decoded and the images have to be created. – Brad Aug 17 '14 at 03:48
  • @TimoTheobald See http://stackoverflow.com/a/9477756/362536 for more information on the drama about FFmpeg and avconv. – Brad Aug 17 '14 at 03:51

1 Answers1

0

The following Python code extracts 60 seconds worth of frames as fast as possible, and outputs them as JPEG files in the current directory.

source

from subprocess import call

call([
    'avconv', '-i', 'video.mp4', 
    '-vsync', '1', 
    '-r', '1',
    '-an', '-y',
    '-t', '60',                 # 60 seconds = 60 pictures
    '%5d.jpg',
])

output

avconv version 0.8.9-6:0.8.9-0ubuntu0.13.10.1, Copyright (c) 2000-2013 the Libav developers
  built on Nov  9 2013 19:09:46 with gcc 4.8.1
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x14bbe00] multiple edit list entries, a/v desync might occur, patch welcome
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf53.21.1
  Duration: 01:59:16.23, start: 0.000000, bitrate: 1153 kb/s
    Stream #0.0(und): Video: mpeg4 (Advanced Simple Profile), yuv420p, 480x368 [PAR 1:1 DAR 30:23], 1016 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 23.98 tbc
    Stream #0.1(und): Audio: aac, 48000 Hz, mono, s16, 63 kb/s
    Stream #0.2(und): Audio: aac, 48000 Hz, mono, s16, 64 kb/s
Incompatible pixel format 'yuv420p' for codec 'mjpeg', auto-selecting format 'yuvj420p'
[buffer @ 0x1670a20] w:480 h:368 pixfmt:yuv420p
[avsink @ 0x147f6a0] auto-inserting filter 'auto-inserted scaler 0' between the filter 'src' and the filter 'out'
[scale @ 0x14bf520] w:480 h:368 fmt:yuv420p -> w:480 h:368 fmt:yuvj420p flags:0x4
Output #0, image2, to '%5d.jpg':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf53.21.1
    Stream #0.0(und): Video: mjpeg, yuvj420p, 480x368 [PAR 1:1 DAR 30:23], q=2-31, 200 kb/s, 90k tbn, 1 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mpeg4 -> mjpeg)
Press ctrl-c to stop encoding
frame=   62 fps= 37 q=11.2 Lsize=      -0kB time=62.00 bitrate=  -0.0kbits/s dup=0 drop=1375    
video:1784kB audio:0kB global headers:0kB muxing overhead -100.001204%
johntellsall
  • 11,853
  • 3
  • 37
  • 32