Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image sequence to mp4 / h264 #471

Closed
sr9yar opened this issue Nov 29, 2018 · 1 comment
Closed

Image sequence to mp4 / h264 #471

sr9yar opened this issue Nov 29, 2018 · 1 comment

Comments

@sr9yar
Copy link

sr9yar commented Nov 29, 2018

Hello,
I'm using your library to put an image sequence into mp4/h264 file.
I'd like to use this format to be able to play the video in a web browser.
Basically I used an example here https://github.com/mikeboers/PyAV/blob/master/examples/encode_frames.py

Here is my code:

import os
import numpy as np
import cv2
import av


#
# example
# https://github.com/mikeboers/PyAV/blob/master/examples/encode_frames.py
#

# fps = '29.97'
fps = '23.976'

# size = (640,360)

# libx264
codec = 'h264'
# codec = 'x264'
# codec = 'mpeg4'

bitrate = 8000000
fmt = 'yuv420p'

def get_filename(i):
  s = str(i)
  while len(s) < 4:
	s = '0' + s
  return '/app/rendering/ffd06183-226c-4320-ad46-ac0a1b03a5cf/' + s + '.png'

output_file = '/app/rendering/ffd06183-226c-4320-ad46-ac0a1b03a5cf/output_av.mp4'
output = av.open(output_file, 'w')

images = [cv2.imread( get_filename(i) ) for i in range(1,35)]


stream = output.add_stream(codec, fps)
stream.bit_rate = bitrate
stream.pix_fmt = fmt

for i, img in enumerate(images):
	# print( img )

	if not i:
		stream.height = img.shape[0]
		stream.width = img.shape[1]

	frame = av.VideoFrame.from_ndarray(img, format='bgr24')
	packet = stream.encode(frame)
	output.mux(packet)

output.close()

print('done \n')

The file is generated ok. When I check file properties, I get:

mediainfo --fullscan output_av.mp4
General
Count                                    : 330
Count of stream of this kind             : 1
Kind of stream                           : General
Kind of stream                           : General
Stream identifier                        : 0
Count of video streams                   : 1
Video_Format_List                        : MPEG-4 Visual
Video_Format_WithHint_List               : MPEG-4 Visual
Codecs Video                             : MPEG-4 Visual
Complete name                            : output_av.mp4
File name                                : output_av.mp4
File name                                : output_av
File extension                           : mp4
Format                                   : MPEG-4
Format                                   : MPEG-4
Format/Extensions usually used           : mov mp4 m4v m4a m4b m4p 3ga 3gpa 3gpp 3gp 3gpp2 3g2 k3g jpm jpx mqv ismv isma ismt f4a f4b f4v
Commercial name                          : MPEG-4
Format profile                           : Base Media
Internet media type                      : video/mp4
Codec ID                                 : isom
Codec ID                                 : isom (isom/iso2/mp41)
Codec ID/Url                             : http://www.apple.com/quicktime/download/standalone.html
CodecID_Compatible                       : isom/iso2/mp41
Codec                                    : MPEG-4
Codec                                    : MPEG-4
Codec/Extensions usually used            : mov mp4 m4v m4a m4b m4p 3ga 3gpa 3gpp 3gp 3gpp2 3g2 k3g jpm jpx mqv ismv isma ismt f4a f4b f4v
File size                                : 300278
File size                                : 293 KiB
File size                                : 293 KiB
File size                                : 293 KiB
File size                                : 293 KiB
File size                                : 293.2 KiB
Duration                                 : 1419
Duration                                 : 1 s 419 ms
Duration                                 : 1 s 419 ms
Duration                                 : 1 s 419 ms
Duration                                 : 00:00:01.419
Duration                                 : 00:00:01;10
Duration                                 : 00:00:01.419 (00:00:01;10)
Overall bit rate mode                    : VBR
Overall bit rate mode                    : Variable
Overall bit rate                         : 1692899
Overall bit rate                         : 1 693 kb/s
Frame rate                               : 23.976
Frame rate                               : 23.976 FPS
Frame count                              : 34
Stream size                              : 1005
Stream size                              : 1 005 Bytes (0%)
Stream size                              : 1 005 Bytes
Stream size                              : 1 005 Bytes
Stream size                              : 1 005 Bytes
Stream size                              : 1 005.0 Bytes
Stream size                              : 1 005 Bytes (0%)
Proportion of this stream                : 0.00335
HeaderSize                               : 36
DataSize                                 : 299281
FooterSize                               : 961
IsStreamable                             : No
File last modification date              : UTC 2018-11-28 14:22:57
File last modification date (local)      : 2018-11-28 17:22:57
Writing application                      : Lavf58.12.100
Writing application                      : Lavf58.12.100

Video
Count                                    : 344
Count of stream of this kind             : 1
Kind of stream                           : Video
Kind of stream                           : Video
Stream identifier                        : 0
StreamOrder                              : 0
ID                                       : 1
ID                                       : 1
Format                                   : MPEG-4 Visual
Commercial name                          : MPEG-4 Visual
Format profile                           : Simple@L1
Format settings, BVOP                    : No
Format settings, BVOP                    : No
Format settings, QPel                    : No
Format settings, QPel                    : No
Format settings, GMC                     : 0
Format settings, GMC                     : No warppoints
Format settings, Matrix                  : Default (H.263)
Format settings, Matrix                  : Default (H.263)
Internet media type                      : video/MP4V-ES
Codec ID                                 : mp4v-20
Codec                                    : MPEG-4V
Codec                                    : MPEG-4 Visual
Codec/Family                             : MPEG-4V
Codec/CC                                 : 20
Codec profile                            : Simple@L1
Codec settings, Packet bitstream         : No
Codec settings, BVOP                     : No
Codec settings, QPel                     : No
Codec settings, GMC                      : 0
Codec settings, GMC                      : No warppoints
Codec settings, Matrix                   : Default (H.263)
Duration                                 : 1419
Duration                                 : 1 s 419 ms
Duration                                 : 1 s 419 ms
Duration                                 : 1 s 419 ms
Duration                                 : 00:00:01.419
Duration                                 : 00:00:01;10
Duration                                 : 00:00:01.419 (00:00:01;10)
Bit rate mode                            : VBR
Bit rate mode                            : Variable
Bit rate                                 : 1688322
Bit rate                                 : 1 688 kb/s
Maximum bit rate                         : 8000000
Maximum bit rate                         : 8 000 kb/s
Width                                    : 640
Width                                    : 640 pixels
Height                                   : 360
Height                                   : 360 pixels
Sampled_Width                            : 640
Sampled_Height                           : 360
Pixel aspect ratio                       : 1.000
Display aspect ratio                     : 1.778
Display aspect ratio                     : 16:9
Rotation                                 : 0.000
Frame rate mode                          : CFR
Frame rate mode                          : Constant
Frame rate                               : 23.976
Frame rate                               : 23.976 (23976/1000) FPS
FrameRate_Num                            : 23976
FrameRate_Den                            : 1000
Frame count                              : 34
Resolution                               : 8
Resolution                               : 8 bits
Colorimetry                              : 4:2:0
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Chroma subsampling                       : 4:2:0
Bit depth                                : 8
Bit depth                                : 8 bits
Scan type                                : Progressive
Scan type                                : Progressive
Interlacement                            : PPF
Interlacement                            : Progressive
Compression mode                         : Lossy
Compression mode                         : Lossy
Bits/(Pixel*Frame)                       : 0.306
Stream size                              : 299273
Stream size                              : 292 KiB (100%)
Stream size                              : 292 KiB
Stream size                              : 292 KiB
Stream size                              : 292 KiB
Stream size                              : 292.3 KiB
Stream size                              : 292 KiB (100%)
Proportion of this stream                : 0.99665
Writing library                          : Lavc58.18.100
Writing library                          : Lavc58.18.100

I expect this to be a h264, but this is not.
After checking out other examples, I assumed I can use h264 for my video.
So, is this possible to make and h264 file?

@jlaine
Copy link
Member

jlaine commented Dec 2, 2018

Your example works fine for me, use ffprobe and it will tell you the output is H.264.

One thing your example is lacking is a "flush" on the output. Also you can just use python's standard formatting mechanisms instead of padding the numbers yourself.

import os
import cv2
import av

images = [cv2.imread('/some/path/%.4d.png' % i) for i in range(1, 35)]

output = av.open('output_av.mp4', 'w')
stream = output.add_stream('h264', '23.976')
stream.bit_rate = 8000000

for i, img in enumerate(images):
    frame = av.VideoFrame.from_ndarray(img, format='bgr24')
    packet = stream.encode(frame)
    output.mux(packet)

# flush
packet = stream.encode(None)
output.mux(packet)

output.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants