|
| 1 | +.. _examples: |
| 2 | + |
| 3 | +******** |
| 4 | +Examples |
| 5 | +******** |
| 6 | + |
| 7 | + |
| 8 | +Converting Image formats |
| 9 | +------------------------ |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + from ffpyplayer.pic import Image, SWScale |
| 14 | + w, h = 500, 100 |
| 15 | + size = w * h * 3 |
| 16 | + buf = bytearray([int(x * 255 / size) for x in range(size)]) |
| 17 | +
|
| 18 | + img = Image(plane_buffers=[buf], pix_fmt='rgb24', size=(w, h)) |
| 19 | + sws = SWScale(w, h, img.get_pixel_format(), ofmt='yuv420p') |
| 20 | +
|
| 21 | + img2 = sws.scale(img) |
| 22 | + img2.get_pixel_format() |
| 23 | + 'yuv420p' |
| 24 | + planes = img2.to_bytearray() |
| 25 | + map(len, planes) |
| 26 | + [50000, 12500, 12500, 0] |
| 27 | +
|
| 28 | +.. _dshow-example: |
| 29 | + |
| 30 | +Playing a webcam with DirectShow on windows |
| 31 | +------------------------------------------- |
| 32 | + |
| 33 | +One can use :meth:`~ffpyplayer.tools.list_dshow_devices` to get a list of the |
| 34 | +devices and their option for playing. For example: |
| 35 | + |
| 36 | +.. code-block:: python |
| 37 | +
|
| 38 | + # see http://ffmpeg.org/ffmpeg-formats.html#Format-Options for rtbufsize |
| 39 | + # lets use the yuv420p, 320x240, 30fps |
| 40 | + # 27648000 = 320*240*3 at 30fps, for 4 seconds. |
| 41 | + # see http://ffmpeg.org/ffmpeg-devices.html#dshow for video_size, and framerate |
| 42 | + lib_opts = {'framerate':'30', 'video_size':'320x240', |
| 43 | + 'pixel_format': 'yuv420p', 'rtbufsize':'27648000'} |
| 44 | + ff_opts = {'f':'dshow'} |
| 45 | + player = MediaPlayer('video=Logitech HD Webcam C525:audio=Microphone (HD Webcam C525)', |
| 46 | + ff_opts=ff_opts, lib_opts=lib_opts) |
| 47 | +
|
| 48 | + while 1: |
| 49 | + frame, val = player.get_frame() |
| 50 | + if val == 'eof': |
| 51 | + break |
| 52 | + elif frame is None: |
| 53 | + time.sleep(0.01) |
| 54 | + else: |
| 55 | + img, t = frame |
| 56 | + print val, t, img.get_pixel_format(), img.get_buffer_size() |
| 57 | + time.sleep(val) |
| 58 | + 0.0 264107.429 rgb24 (230400, 0, 0, 0) |
| 59 | + 0.0 264108.364 rgb24 (230400, 0, 0, 0) |
| 60 | + 0.0790016651154 264108.628 rgb24 (230400, 0, 0, 0) |
| 61 | + 0.135997533798 264108.764 rgb24 (230400, 0, 0, 0) |
| 62 | + 0.274529457092 264108.897 rgb24 (230400, 0, 0, 0) |
| 63 | + 0.272421836853 264109.028 rgb24 (230400, 0, 0, 0) |
| 64 | + 0.132406949997 264109.164 rgb24 (230400, 0, 0, 0) |
| 65 | + ... |
| 66 | +
|
| 67 | + # NOTE, by default the output was rgb24. To keep the output format the |
| 68 | + # same as the input, do ff_opts['out_fmt'] = 'yuv420p' |
| 69 | +
|
| 70 | +
|
| 71 | +Saving an image to disk |
| 72 | +----------------------- |
| 73 | + |
| 74 | +.. code-block:: python |
| 75 | +
|
| 76 | + from ffpyplayer.pic import Image, SWScale |
| 77 | + from ffpyplayer.tools import get_supported_pixfmts |
| 78 | +
|
| 79 | + # create image |
| 80 | + w, h = 500, 100 |
| 81 | + fmt = 'rgb24' |
| 82 | + size = w * h * 3 |
| 83 | + buf = bytearray([int(x * 255 / size) for x in range(size)]) |
| 84 | + img = Image(plane_buffers=[buf], pix_fmt=fmt, size=(w, h)) |
| 85 | + codec = 'tiff' # we'll encode it using the tiff codec |
| 86 | +
|
| 87 | + # make sure the output codec supports the input pixel format type |
| 88 | + # otherwise, convert it to the best pixel format |
| 89 | + ofmt = get_supported_pixfmts(codec, fmt)[0] |
| 90 | + if ofmt != fmt: |
| 91 | + sws = SWScale(w, h, fmt, ofmt=ofmt) |
| 92 | + img = sws.scale(img) |
| 93 | + fmt = ofmt |
| 94 | +
|
| 95 | + out_opts = {'pix_fmt_in': fmt, 'width_in': w, 'height_in': h, |
| 96 | + 'frame_rate': (30, 1), 'codec': codec} |
| 97 | + writer = MediaWriter('myfile.tiff', [out_opts]) |
| 98 | + writer.write_frame(img=img, pts=0, stream=0) |
| 99 | + writer.close() |
| 100 | +
|
| 101 | + # to save the file as a compressed tiff using lzw |
| 102 | + writer = MediaWriter('myfile.tiff', [out_opts], lib_opts={'compression_algo': 'lzw'}) |
| 103 | + writer.write_frame(img=img, pts=0, stream=0) |
| 104 | + writer.close() |
| 105 | +
|
| 106 | +Simple transcoding example |
| 107 | +-------------------------- |
| 108 | + |
| 109 | +.. code-block:: python |
| 110 | +
|
| 111 | + from ffpyplayer.player import MediaPlayer |
| 112 | + from ffpyplayer.writer import MediaWriter |
| 113 | + import time, weakref |
| 114 | +
|
| 115 | + # only video |
| 116 | + ff_opts={'an':True, 'sync':'video'} |
| 117 | + player = MediaPlayer(filename, ff_opts=ff_opts) |
| 118 | + # wait for size to be initialized (todo: add timeout and check for quitting) |
| 119 | + while player.get_metadata()['src_vid_size'] == (0, 0): |
| 120 | + time.sleep(0.01) |
| 121 | +
|
| 122 | + frame_size = player.get_metadata()['src_vid_size'] |
| 123 | + # use the same size as the inputs |
| 124 | + out_opts = {'pix_fmt_in':'rgb24', 'width_in':frame_size[0], |
| 125 | + 'height_in':frame_size[1], 'codec':'rawvideo', |
| 126 | + 'frame_rate':(30, 1)} |
| 127 | +
|
| 128 | + writer = MediaWriter(filename_out, [out_opts]) |
| 129 | + while 1: |
| 130 | + frame, val = player.get_frame() |
| 131 | + if val == 'eof': |
| 132 | + break |
| 133 | + elif frame is None: |
| 134 | + time.sleep(0.01) |
| 135 | + else: |
| 136 | + img, t = frame |
| 137 | + writer.write_frame(img=img, pts=t, stream=0) |
| 138 | +
|
| 139 | +More complex transcoding example |
| 140 | +-------------------------------- |
| 141 | + |
| 142 | +.. code-block:: python |
| 143 | +
|
| 144 | + from ffpyplayer.player import MediaPlayer |
| 145 | + from ffpyplayer.tools import free_frame_ref |
| 146 | + from ffpyplayer.writer import MediaWriter |
| 147 | + import time, weakref |
| 148 | +
|
| 149 | + # only video, output yuv420p frames |
| 150 | + ff_opts={'an':True, 'sync':'video', 'out_fmt':'yuv420p'} |
| 151 | + player = MediaPlayer(filename, ff_opts=ff_opts) |
| 152 | + # wait for size to be initialized |
| 153 | + while player.get_metadata()['src_vid_size'] == (0, 0): |
| 154 | + time.sleep(0.01) |
| 155 | +
|
| 156 | + frame_size = player.get_metadata()['src_vid_size'] |
| 157 | + # use the half the size for the output as the input |
| 158 | + out_opts = {'pix_fmt_in':'yuv420p', 'width_in':frame_size[0], |
| 159 | + 'height_in':frame_size[1], 'codec':'rawvideo', |
| 160 | + 'frame_rate':(30, 1), 'width_out':frame_size[0] / 2, |
| 161 | + 'height_out':frame_size[1] / 2} |
| 162 | +
|
| 163 | + writer = MediaWriter(filename_out, [out_opts]) |
| 164 | + while 1: |
| 165 | + frame, val = player.get_frame() |
| 166 | + if val == 'eof': |
| 167 | + break |
| 168 | + elif frame is None: |
| 169 | + time.sleep(0.01) |
| 170 | + else: |
| 171 | + img, t = frame |
| 172 | + writer.write_frame(img=img, pts=t, stream=0) |
| 173 | +
|
| 174 | +.. _write-simple: |
| 175 | + |
| 176 | +Writing video to file |
| 177 | +--------------------- |
| 178 | + |
| 179 | +.. code-block:: python |
| 180 | +
|
| 181 | + from ffpyplayer.writer import MediaWriter |
| 182 | + from ffpyplayer.pic import Image |
| 183 | +
|
| 184 | + w, h = 640, 480 |
| 185 | + # write at 5 fps. |
| 186 | + out_opts = {'pix_fmt_in':'rgb24', 'width_in':w, 'height_in':h, 'codec':'rawvideo', |
| 187 | + 'frame_rate':(5, 1)} |
| 188 | + # write using rgb24 frames into a two stream rawvideo file where the output |
| 189 | + # is half the input size for both streams. Avi format will be used. |
| 190 | + writer = MediaWriter('output.avi', [out_opts] * 2, width_out=w/2, |
| 191 | + height_out=h/2) |
| 192 | +
|
| 193 | + # Construct images |
| 194 | + size = w * h * 3 |
| 195 | + buf = bytearray([int(x * 255 / size) for x in range(size)]) |
| 196 | + img = Image(plane_buffers=[buf], pix_fmt='rgb24', size=(w, h)) |
| 197 | +
|
| 198 | + buf = bytearray([int((size - x) * 255 / size) for x in range(size)]) |
| 199 | + img2 = Image(plane_buffers=[buf], pix_fmt='rgb24', size=(w, h)) |
| 200 | +
|
| 201 | + for i in range(20): |
| 202 | + writer.write_frame(img=img, pts=i / 5., stream=0) # stream 1 |
| 203 | + writer.write_frame(img=img2, pts=i / 5., stream=1) # stream 2 |
| 204 | +
|
| 205 | +Or force an output format of avi, even though the filename is .mp4.: |
| 206 | + |
| 207 | +.. code-block:: python |
| 208 | +
|
| 209 | + writer = MediaWriter('output.mp4', [out_opts] * 2, fmt='avi', |
| 210 | + width_out=w/2, height_out=h/2) |
| 211 | +
|
| 212 | +.. _write-h264: |
| 213 | + |
| 214 | +Compressing video to h264 |
| 215 | +------------------------- |
| 216 | + |
| 217 | +Or writing compressed h264 files (notice the file is now only 5KB, while |
| 218 | +the above results in a 10MB file): |
| 219 | + |
| 220 | +.. code-block:: python |
| 221 | +
|
| 222 | + from ffpyplayer.writer import MediaWriter |
| 223 | + from ffpyplayer.tools import get_supported_pixfmts, get_supported_framerates |
| 224 | + from ffpyplayer.pic import Image |
| 225 | +
|
| 226 | + # make sure the pixel format and rate are supported. |
| 227 | + print get_supported_pixfmts('libx264', 'rgb24') |
| 228 | + #['yuv420p', 'yuvj420p', 'yuv422p', 'yuvj422p', 'yuv444p', 'yuvj444p', 'nv12', 'nv16'] |
| 229 | + print get_supported_framerates('libx264', (5, 1)) |
| 230 | + #[] |
| 231 | + w, h = 640, 480 |
| 232 | + out_opts = {'pix_fmt_in':'rgb24', 'width_in':w, 'height_in':h, 'codec':'libx264', |
| 233 | + 'frame_rate':(5, 1)} |
| 234 | +
|
| 235 | + # use the following libx264 compression options |
| 236 | + lib_opts = {'preset':'slow', 'crf':'22'} |
| 237 | + # set the following metadata (ffmpeg doesn't always support writing metadata) |
| 238 | + metadata = {'title':'Singing in the sun', 'author':'Rat', 'genre':'Animal sounds'} |
| 239 | +
|
| 240 | + # write using yuv420p frames into a two stream h264 codec, mp4 file where the output |
| 241 | + # is half the input size for both streams. |
| 242 | + writer = MediaWriter('output.avi', [out_opts] * 2, fmt='mp4', |
| 243 | + width_out=w/2, height_out=h/2, pix_fmt_out='yuv420p', |
| 244 | + lib_opts=lib_opts, metadata=metadata) |
| 245 | +
|
| 246 | + # Construct images |
| 247 | + size = w * h * 3 |
| 248 | + buf = bytearray([int(x * 255 / size) for x in range(size)]) |
| 249 | + img = Image(plane_buffers=[buf], pix_fmt='rgb24', size=(w, h)) |
| 250 | +
|
| 251 | + buf = bytearray([int((size - x) * 255 / size) for x in range(size)]) |
| 252 | + img2 = Image(plane_buffers=[buf], pix_fmt='rgb24', size=(w, h)) |
| 253 | +
|
| 254 | + for i in range(20): |
| 255 | + writer.write_frame(img=img, pts=i / 5., stream=0) # stream 1 |
| 256 | + writer.write_frame(img=img2, pts=i / 5., stream=1) # stream 2 |
0 commit comments