Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

# -*- coding: utf-8 -*- 

""" 

@file 

@brief Quelques questions d'ordre général autour du langage Python. 

""" 

from contextlib import redirect_stdout, redirect_stderr 

import io 

import os 

import sys 

import numpy 

import tempfile 

import time 

from pytube import YouTube 

from imageio import imsave 

import moviepy.audio.fx.all as afx 

import moviepy.video.fx.all as vfx 

from moviepy.video.VideoClip import ImageClip, VideoClip 

from moviepy.video.io.ImageSequenceClip import ImageSequenceClip 

from moviepy.audio.AudioClip import CompositeAudioClip 

from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip 

from moviepy.video.compositing.concatenate import concatenate_videoclips 

from moviepy.audio.AudioClip import concatenate_audioclips 

from .moviepy_context import AudioContext, VideoContext 

from PIL import Image, ImageFont, ImageDraw 

from skimage.io._plugins.pil_plugin import pil_to_ndarray 

from skimage.transform import rescale 

 

 

class FontError(Exception): 

""" 

Raised when a font cannot be found. 

""" 

pass 

 

 

def check(fLOG=None): 

""" 

Checks a couple of functionality works. 

The test takes 5-6 seconds to download, 

4-5 seconds to process the video. 

 

@param logging function 

""" 

t1 = time.clock() 

with tempfile.TemporaryDirectory() as temp: 

if fLOG: 

fLOG('[check] download_youtube_video') 

vid = download_youtube_video("4o5baMYWdtQ", temp, res=None) 

vid = os.path.join(temp, vid) 

t2 = time.clock() 

if fLOG: 

fLOG('[check] video_compose') 

ext = video_compose(vid, vid, t2=2, place="h2") 

dest = os.path.join(temp, "res.mp4") 

if fLOG: 

fLOG('[check] video_save') 

video_save(ext, dest) 

res = os.path.exists(dest) 

delta1 = time.clock() - t1 

delta2 = time.clock() - t2 

if fLOG: 

fLOG("[check] video time={0} - video={1}".format(delta1, delta2)) 

return res 

 

 

########## 

# youtube 

########## 

 

 

def download_youtube_video(tag, output_path=None, res='720p', mime_type="video/mp4", **kwargs): 

""" 

Downloads a video from :epkg:`youtube` with :epkg:`pytube`. 

Télécharge une vidéo depuis :epkg:`youtube` avec :epkg:`pytube`. 

 

@param tag tag of the :epkg:`youtube` video to download 

@param output_path output path 

@param mime_type see :epkg:`youtube` 

@param res see :epkg:`youtube` 

@param kwargs see :epkg:`youtube` 

@return filename (relative to *output_path*) 

 

.. faqref:: 

:title: Télécharger une vidéo sur YouTube 

 

Le module :epkg:`pytube` permet de télécharger une vidéo 

:epkg:`youtube`. Chaque vidéo est disponible selon plusieurs 

format dont on récupère la liste avant de choisir 

qui correspond à celui voulu. 

 

:: 

 

from pytube import YouTube 

yt = YouTube('https://www.youtube.com/watch?v=tRFHXMQP-QU') 

st = yt.streams 

fil = st.filter(mime_type="video/mp4", res="720p") 

fil.first().download() 

 

""" 

yt = YouTube('https://www.youtube.com/watch?v={0}'.format(tag)) 

st = yt.streams.filter(mime_type=mime_type, res=res, **kwargs) 

fi = st.first() 

if fi is None: 

raise ValueError("By default the function downloads a video with resolution = 720, " + 

"if it is not available, switch to res=None " + 

"to choose the first one available.") 

fi.download(output_path=output_path) 

return fi.default_filename 

 

######## 

# audio 

######## 

 

 

def audio_extract_audio(audio_or_file, ta=0, tb=None): 

""" 

Extracts a part of an audio. 

Extrait une partie du son. 

Uses `subclip <https://zulko.github.io/moviepy/ref/AudioClip.html?highlight=audioclip#moviepy.audio.AudioClip.AudioClip.subclip>`_. 

 

@param audio_or_file string or :epkg:`AudioClip` 

@param ta beginning 

@param tb end 

@return :epkg:`VideoClip` 

 

Example: 

 

:: 

 

from code_beatrix.faq.faq_video import audio_extract_audio 

son = audio_extract_audio('son.mp3', '00:00:01', '00:00:02') 

""" 

with AudioContext(audio_or_file) as audio: 

return audio.subclip(ta, tb) 

 

 

def audio_save(audio_or_file, filename, verbose=False, **kwargs): 

""" 

Saves as a sound. 

Enregistre un son dans un fichier. 

Uses `write_audiofile <https://zulko.github.io/moviepy/ref/AudioClip.html?highlight=audioclip#moviepy.audio.AudioClip.AudioClip.write_audiofile>`_. 

 

@param audio_or_file string or :epkg:`AudioClip` 

@param filename save into this filename 

@param verbose logging or not 

@param kwargs see `write_audiofile <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=videofileclip#moviepy.video.io.VideoFileClip.VideoFileClip.write_videofile>`_ 

""" 

with AudioContext(audio_or_file) as audio: 

if verbose: 

audio.write_audiofile(filename, verbose=verbose, **kwargs) 

else: 

f = io.StringIO() 

with redirect_stdout(f): 

with redirect_stderr(f): 

audio.write_audiofile(filename, verbose=verbose, **kwargs) 

 

 

def audio_modification(audio, loop_duration=None, volumex=1., 

fadein=False, fadeout=False, t_start=0, t_end=None, 

speed=1.): 

""" 

Modifies a sound. 

Modifie un son. 

 

@param audio sound 

@param loop_duration loops sound 

@param volumex multiplies the sound 

@param fadein decreases the volume of the first seconds 

@param fadeout decreases the volume of the last seconds 

@param t_start shorten the audio 

@param t_end shorten the audio 

@param speed speed of the sound 

@return new sound 

""" 

with AudioContext(audio) as audio: 

if speed: 

audio = audio.fl_time(lambda t: t * speed, keep_duration=True) 

# wav = audio.to_soundarray(fps=audio.fps) 

# audio = AudioArrayClip(wav, audio.fps) 

if volumex != 1.: 

audio = audio.fx(afx.volumex, volumex) 

if loop_duration: 

audio = afx.audio_loop(audio, duration=loop_duration) 

if fadein: 

audio = audio.fx(afx.audio_fadein, 1.0) 

if fadeout: 

audio = audio.fx(afx.audio_fadeout, 1.0) 

if t_start != 0 or t_end is not None: 

audio = audio.subclip(t_start=t_start, t_end=t_end) 

return audio 

 

 

def audio_compose(audio_or_file1, audio_or_file2, t1=0, t2=None): 

""" 

Concatenates or superposes two sounds. 

Ajoute ou superpose deux sons. 

 

@param audio_or_file1 son 1 

@param audio_or_file2 son 2 

@param t1 start of the first sound 

@param t2 start of the second sound (or None to add it ad 

@return new sound 

 

Example: 

 

:: 

 

from code_beatrix.faq.faq_video import audio_compose 

son = audio_compose('son1.mp3', 'son2.mp3', 0, 10) 

""" 

with AudioContext(audio_or_file1) as audio1: 

with AudioContext(audio_or_file2) as audio2: 

add = [] 

if t1 != 0: 

add.append(audio1.set_start(t1)) 

else: 

add.append(audio1) 

if t2 is None: 

add.append(audio2.set_start(audio1.duration + t1)) 

else: 

add.append(audio2.set_start(t2)) 

return CompositeAudioClip(add) 

 

 

def audio_concatenate(audio_or_files, **kwargs): 

""" 

Concatenates sounds. 

Met bout à bout des sons. 

 

@param audio_or_files list of sounds or filenames 

@param kwargs additional parameters for 

`concatenate_audioclips <https://github.com/Zulko/moviepy/blob/master/moviepy/audio/AudioClip.py#L308>`_ 

@return :epkg:`VideoClip` 

 

Example: 

 

:: 

 

from code_beatrix.faq.faq_video import audio_concatenate 

son = audio_concatenate('son1.mp3', 'son2.mp3') 

""" 

ctx = [AudioContext(_).__enter__() for _ in audio_or_files] 

res = concatenate_audioclips([_.audio for _ in ctx], **kwargs) 

for _ in ctx: 

_.__exit__() 

return res 

 

######## 

# vidéo 

######## 

 

 

def video_extract_video(video_or_file, ta=0, tb=None): 

""" 

Extracts a part of a video. 

Extrait une partie de la vidéo. 

Uses `subclip <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=videofileclip#moviepy.video.VideoClip.VideoClip.subclip>`_. 

 

@param video_or_file string or :epkg:`VideoClip` 

@param ta beginning 

@param tb end 

@return :epkg:`VideoClip` 

 

Example: 

 

:: 

 

from code_beatrix.faq_faq_video import video_extract_video 

vid = video_extract_video('exemple.mp4', '00:00:01', '00:00:04') 

""" 

with VideoContext(video_or_file) as video: 

return video.subclip(ta, tb) 

 

 

def video_save(video_or_file, filename, verbose=False, duration=None, **kwargs): 

""" 

Saves as a video or as a :epkg:`gif`. 

Enregistre une vidéo dans un fichier. 

Uses `write_videofile <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=videofileclip#moviepy.video.io.VideoFileClip.VideoFileClip.write_videofile>`_. 

 

@param video_or_file string or :epkg:`VideoClip` 

@param filename video saved into this filename 

@param duration overwrite duration, 

see method `set_duration <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=videoclip#moviepy.video.VideoClip.VideoClip.set_duration>`_ 

@param verbose logging or not 

@param kwargs see `write_videofile <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=videofileclip#moviepy.video.io.VideoFileClip.VideoFileClip.write_videofile>`_ 

 

Example: 

 

:: 

 

from code_beatrix.faq_faq_video import video_extract_video, video_save 

vid = video_extract_video('exemple.mp4', '00:00:01', '00:00:04') 

video_save(vid, 'new_video.mp4') 

""" 

if isinstance(filename, str) and os.path.splitext(filename)[-1] == '.gif': 

with VideoContext(video_or_file) as video: 

if duration is not None: 

video = video.set_duration(duration) 

if verbose: 

video.write_gif(filename, verbose=verbose, **kwargs) 

else: 

f = io.StringIO() 

with redirect_stdout(f): 

with redirect_stderr(f): 

video.write_gif(filename, verbose=verbose, **kwargs) 

else: 

with VideoContext(video_or_file) as video: 

if duration is not None: 

video = video.set_duration(duration) 

if verbose: 

video.write_videofile(filename, verbose=verbose, **kwargs) 

else: 

f = io.StringIO() 

with redirect_stdout(f): 

with redirect_stderr(f): 

video.write_videofile( 

filename, verbose=verbose, **kwargs) 

 

 

def video_enumerate_frames(video_or_file, folder=None, fps=10, pattern='images_%04d.jpg', **kwargs): 

""" 

Enumerates frames from a video. 

Itère sur des images depuis une vidéo. 

 

@param video_or_file string or :epkg:`VideoClip` 

@param folder where to exports the images or returns arrays if None 

@param pattern image names 

@param fps frames per seconds 

@param kwargs arguments to `iter_frames <https://zulko.github.io/moviepy/ref/AudioClip.html?highlight=frames#moviepy.audio.AudioClip.AudioClip.iter_frames>`_ 

@return iterator on arrays or files (see parameter *folder*) 

 

Example: 

 

:: 

 

form code_beatrix.faq.faq_video import video_enumerate_frames 

vid = 'example.mp4') 

for frame in video_enumerate_frames(vid, folder=temp): 

# ... 

 

""" 

with VideoContext(video_or_file) as video: 

if folder is None: 

for frame in video.iter_frames(fps=fps, **kwargs): 

yield frame 

else: 

if 'dtype' in kwargs: 

if kwargs['dtype'] != 'uint8': 

raise ValueError("dtype must be uint8") 

else: 

del kwargs['dtype'] 

 

for i, frame in enumerate(video.iter_frames(fps=fps, dtype='uint8', **kwargs)): 

# saves as image 

name = os.path.join(folder, pattern % i) 

imsave(name, frame) 

yield name 

 

 

def video_replace_audio(video_or_file, new_sound, **kwargs): 

""" 

Replaces the sound of a video. 

Remplace la bande-son d'une vidéo. 

 

@param video_or_file string or :epkg:`VideoClip` 

@param new_sound sound 

@param kwargs see @see fn audio_modification 

@return :epkg:`VideoClip` 

 

The list of available transformations is at: 

`vfx <https://zulko.github.io/moviepy/ref/videofx.html?highlight=vfx>`_. 

If parameter ``loop=True`` is specified, 

*loop_duration* becomes the duration of the video. 

 

Example: 

 

:: 

 

from code_beatrix.faq.faq_video import video_replace_sound 

vid = video_replace_sound('video.mp4', 'son.mp3', loop=True, volumex=5.5, t_end='00:00:05') 

""" 

with VideoContext(video_or_file) as video: 

if 'loop' in kwargs: 

kwargs['loop_duration'] = video.duration 

del kwargs['loop'] 

audio = audio_modification(new_sound, **kwargs) 

new_clip = video.set_audio(audio) 

return new_clip 

 

 

def video_extract_audio(video_or_file): 

""" 

Returns the audio of a video. 

Retourne le son d'une vidéo. 

 

@param video_or_file string or :epkg:`VideoClip` 

@return :epkg:`AudioClip` 

""" 

with VideoContext(video_or_file) as video: 

return video.audio 

 

 

def video_remove_audio(video_or_file): 

""" 

Returns the same video without audio. 

Retourne la même vidéo sans le son. 

 

@param video_or_file string or :epkg:`VideoClip` 

@return :epkg:`AudioClip` 

""" 

with VideoContext(video_or_file) as video: 

return video.without_audio() 

 

 

def video_compose(video_or_file1, video_or_file2=None, t1=0, t2=0, place=None, **kwargs): 

""" 

Concatenates or superposes two videos. 

Ajoute ou superpose deux vidéos. 

 

@param video_or_file1 vidéo 1 or list of video 

@param video_or_file2 vidéo 2 

@param t1 start of the first sound 

@param t2 start of the second sound (or None to add it ad 

@param place predefined placements 

@param kwargs additional parameters, 

sent to `CompositeVideoClip <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=compositevideoclip#compositevideoclip>`_ 

@return :epkg:`VideoClip` 

 

Example: 

 

:: 

 

from code_beatrix.faq.faq_video import video_compose 

vid = video_compose('video1.mp4', 'video2.mp4', '00:00:01', '00:00:04') 

 

The first video defines the size of the final video. 

List of predefined placements: 

 

* *h2*: two videos side by side horizontally 

* *v2*: two videos side by side vertically 

* *br*: two videos, second is placed at the bottom right corner 

 

*zoom* can be defined as a argument, it applies on the second 

video if *place* is defined and if there are two videos. 

""" 

if place is None: 

if isinstance(video_or_file1, list): 

if video_or_file2 is not None: 

raise ValueError( 

'video_or_file1 is a list, video_or_file2 should be None') 

vids = [VideoContext(i).__enter__() for i in video_or_file1] 

comp = [] 

for i, v in enumerate(vids): 

v = v.video 

if isinstance(t1, list) and i < len(t1): 

v.set_start(t1[i]) 

comp.append(v) 

res = CompositeVideoClip(comp, **kwargs) 

for v in vids: 

v.__exit__() 

return res 

else: 

with VideoContext(video_or_file1) as video1: 

with VideoContext(video_or_file2) as video2: 

add = [] 

if t1 != 0: 

add.append(video1.set_start(t1)) 

else: 

add.append(video1) 

if t2 is None: 

add.append(video2.set_start(video1.duration + t1)) 

else: 

add.append(video2.set_start(t2)) 

return CompositeVideoClip(add, **kwargs) 

else: 

 

def get_two(video_or_file1, video_or_file2, t1, t2): 

if isinstance(video_or_file1, list): 

if len(video_or_file1) != 2: 

raise ValueError( 

"Expecting two videos not {0}".format(len(video_or_file1))) 

v1, v2 = video_or_file1 

t1, t2 = t1 

else: 

if len(video_or_file2) is None: 

raise ValueError("Expecting two videos not less") 

v1, v2 = video_or_file1, video_or_file2 

vc1 = VideoContext(v1).__enter__() 

vc2 = VideoContext(v2).__enter__() 

return (vc1, vc2), (t1, t2) 

 

(vc1, vc2), (t1, t2) = get_two(video_or_file1, video_or_file2, t1, t2) 

 

v1 = vc1.video 

v2 = vc2.video 

 

if kwargs.get('zoom', 1.) != 1.: 

v2 = video_modification(v2, resize=kwargs['zoom']) 

del kwargs['zoom'] 

 

# Predefined placements. 

if place == "h2": 

pos1 = 0, 0 

pos2 = v1.size[0], 0 

v1 = video_position(v1, pos=pos1) 

v2 = video_position(v2, pos=pos2) 

if 'size' not in kwargs: 

kwargs['size'] = v1.size[0] + \ 

v2.size[0], max(v1.size[1], v2.size[1]) 

res = video_compose(v1, v2, t1, t2, **kwargs) 

elif place == "v2": 

pos1 = 0, 0 

pos2 = 0, v1.size[1] 

v1 = video_position(v1, pos=pos1) 

v2 = video_position(v2, pos=pos2) 

if 'size' not in kwargs: 

kwargs['size'] = max(v1.size[0], v2.size[0] 

), v1.size[1] + v2.size[1] 

res = video_compose(v1, v2, t1, t2, **kwargs) 

elif place == "br": 

pos1 = 0, 0 

pos2 = max(0, v1.size[0] - v2.size[0] 

), max(0, v1.size[1] - v2.size[1]) 

v1 = video_position(v1, pos=pos1) 

v2 = video_position(v2, pos=pos2) 

if 'size' not in kwargs: 

kwargs['size'] = v1.size[0], v1.size[1] 

res = video_compose(v1, v2, t1, t2, **kwargs) 

else: 

raise ValueError("Unknown placement '{0}'".format(place)) 

 

vc1.__exit__() 

vc2.__exit__() 

return res 

 

 

def video_concatenate(video_or_files, **kwargs): 

""" 

Concatenates videos. 

Met bout à bout des vidéos. 

 

@param video_or_files list of videos or filenames 

@param kwargs additional parameters for 

`concatenate_videoclips <https://github.com/Zulko/moviepy/blob/master/moviepy/video/compositing/concatenate.py#L15>`_ 

@return :epkg:`VideoClip` 

""" 

ctx = [VideoContext(_).__enter__() for _ in video_or_files] 

res = concatenate_videoclips([_.video for _ in ctx], **kwargs) 

for _ in ctx: 

_.__exit__() 

return res 

 

 

def video_modification(video_or_file, volumex=1., resize=1., speed=1., 

mirrorx=False, mirrory=False): 

""" 

Modifies a video. 

Modifie une vidéo. 

 

@param video_or_file string or :epkg:`VideoClip` 

@param volumex multiplies the sound 

@param speed speed of the sound 

@param resize resize 

@param mirrorx mirror x 

@param mirrory mirror y 

@return new video 

 

Example: 

 

:: 

 

from code_beatrix.faq.faq_video import video_modification 

vid = video_modification('video.mp4', speed=2., mirrory=True, mirrorx=True) 

""" 

def check_duration(video): 

if video.duration is None: 

raise ValueError('video duration should not be None') 

 

with VideoContext(video_or_file) as video: 

if speed: 

check_duration(video) 

dur = video.duration 

video = video.fl_time(lambda t: t * speed) 

video = video.set_duration(dur / speed) 

if volumex != 1.: 

video = video.fx(vfx.volumex, volumex) 

if resize != 1.: 

video = video.fx(vfx.resize, resize) 

if mirrorx: 

video = video.fx(vfx.mirror_x) 

if mirrory: 

video = video.fx(vfx.mirror_y) 

return video 

 

 

def video_image(image_or_file, duration=None, zoom=None, opacity=None, **kwargs): 

""" 

Creates a :epkg:`ImageClip`. 

Créé une vidéo à partir d'une image. 

 

@param image_or_file image or file 

@param duration duration or None if not known 

@param zoom applies a zoom on the image 

@param opacity opacity of the image (0 for transparent, 255 for opaque) 

@param kwargs additional parameters for :epkg:`ImageClip` 

@return :epkg:`ImageClip` 

 

If *duration* is None, it will be fixed when the image is 

composed with another one. The image remains wherever it is placed. 

""" 

if isinstance(image_or_file, str): 

img = Image.open(image_or_file) 

return video_image(img, duration=duration, zoom=zoom, opacity=opacity, **kwargs) 

elif isinstance(image_or_file, numpy.ndarray): 

if zoom is not None: 

img = rescale(image_or_file, zoom) 

return video_image(img, duration=duration, opacity=opacity, **kwargs) 

else: 

img = image_or_file 

if len(img.shape) != 3: 

raise ValueError( 

"Image is not RGB or RGBA shape={0}".format(img.shape)) 

if img.shape[2] == 3: 

pilimg = Image.fromarray(img).convert('RGBA') 

img = pil_to_ndarray(pilimg) 

if opacity is None: 

opacity = 255 

if isinstance(opacity, int): 

img[:, :, 3] = opacity 

elif isinstance(opacity, float): 

img[:, :, 3] = int(opacity * 255) 

elif opacity is not None: 

raise TypeError("opacity should be int or float or None") 

return ImageClip(img, duration=duration, transparent=True, **kwargs) 

elif isinstance(image_or_file, Image.Image): 

if image_or_file.mode != 'RGBA': 

image_or_file = image_or_file.convert('RGBA') 

if zoom is not None: 

image_or_file = image_or_file.resize(zoom) 

img = pil_to_ndarray(image_or_file) 

return video_image(img, duration=duration, opacity=opacity, **kwargs) 

else: 

raise TypeError( 

"Unable to create a video from type {0}".format(type(image_or_file))) 

 

 

def video_position(video_or_file, pos, relative=False): 

""" 

Modifies the position of a position. 

Modifie la position d'une video. 

Relies on function 

`set_position <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=imageclip#moviepy.video.VideoClip.VideoClip.set_position>`_. 

 

@param video_or_file string or :epkg:`VideoClip` 

@param pos see `set_position <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=imageclip#moviepy.video.VideoClip.VideoClip.set_position>`_ 

@param relative see `set_position <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=imageclip#moviepy.video.VideoClip.VideoClip.set_position>`_ 

@return :epkg:`VideoClip` 

 

This function moves the video inside another one. 

Therefore, it has no effect if the result of this video 

is composed. See function @see fn video_compose. 

Example: 

 

:: 

 

from code_beatrix.faq.faq_video import video_image, video_position, video_compose, video_text 

 

img = 'GastonLagaffe_1121.jpg' 

vidimg = video_image(img, duration=5, opacity=200) 

vidimg = video_position(vidimg, lambda t: (0, 0), relative=True) 

 

text = video_text('boule', size=2., color=(255, 0, 0, 128), background=(0, 255, 0, 100)) 

text = video_position(text, lambda t: (t * 0.1, t * 0.2), relative=True) 

 

comb = video_compose([vidimg, text], t1=[0, 1]) 

 

You can see an example of the video it produces in notebook 

:ref:`video_notebook`. 

""" 

with VideoContext(video_or_file) as video: 

video = video.set_position(pos=pos, relative=relative) 

return video 

 

 

def video_resize(video_or_file, newsize): 

""" 

Resizes a video. 

Modifie la taille d'une video. 

Relies on function 

`resize <https://zulko.github.io/moviepy/ref/videofx/moviepy.video.fx.all.resize.html#moviepy.video.fx.all.resize>`_. 

 

@param video_or_file string or :epkg:`VideoClip` 

@param newsize `resize <https://zulko.github.io/moviepy/ref/videofx/moviepy.video.fx.all.resize.html#moviepy.video.fx.all.resize>`_ 

@return :epkg:`VideoClip` 

""" 

with VideoContext(video_or_file) as video: 

video = video.resize(newsize) 

return CompositeVideoClip([video]) 

 

 

def video_text(text, font=None, fontsize=32, size=None, 

color=None, background=None, opacity=None, 

**kwargs): 

""" 

Creates an image with text (:epkg:`ImageClip`). 

Créé une image à partir de texte. 

 

@param text text 

@param color color 

@param font police name 

@param fontsize font size 

@param size image size, None to get the smallest one which 

contains the text, a float to get *size* times 

this smallest size 

@param background background of the image 

@param opacity to overwrite the opacity, 

*color* and *background* should be 4-uple colors, 

the last number in ``(0, 0, 0, 255)`` represents the 

opacity 

@param kwargs additional parameters sent to @see fn video_image 

@return :epkg:`ImageClip` 

 

If *duration* is None, it will be fixed when the image is 

composed with another one. The image remains wherever it is placed. 

The *opacity* is a number between 0 (transparent) and 255 (opaque). 

0 means the image cannot be seen. The number can be set up for each 

pixel. By default, the image background is transparent (0). You can find 

many font at `google/fonts <https://github.com/google/fonts/tree/master/ofl>`_ 

or `msfonts <https://github.com/caarlos0-graveyard/msfonts/tree/master/fonts>`_. 

""" 

if background is None: 

background = (255, 255, 255, 0) 

if color is None: 

color = (0, 0, 0, 255) 

if isinstance(font, str): 

if not font.endswith('.ttf'): 

font += '.ttf' 

elif font is None: 

if sys.platform.startswith('win'): 

font = "arial.ttf" 

else: 

exp = '/usr/share/fonts/truetype/dejavu' # os.path.expanduser('~') 

d = exp # os.path.join(exp, '.local', 'share', 'fonts') 

if not os.path.exists(d): 

raise FileNotFoundError("Unable to find '{0}'".format(d)) 

font = os.path.join(d, "DejaVuSans.ttf") 

if not os.path.exists(font): 

raise FileNotFoundError("Unable to find font '{0}'. Available:\n{1}".format( 

font, "\n".join(os.listdir(d)))) 

try: 

obj = ImageFont.truetype(font=font, size=fontsize) 

except OSError as e: 

raise FontError("Unable to find font '{0}'".format(font)) from e 

if size is None: 

size = obj.getsize(text) 

elif isinstance(size, (float, int)): 

fs = obj.getsize(text) 

size = (int(fs[0] * size), int(fs[1] * size)) 

elif not isinstance(size, tuple): 

raise TypeError("size should be a tuple or a float") 

if opacity is not None: 

if len(color) == 3: 

color = color + (opacity,) 

elif len(color) == 4: 

color = color[:3] + (opacity,) 

else: 

raise ValueError("color should a 3 or 4 tuple") 

img = Image.new('RGBA', size, background) 

draw = ImageDraw.Draw(img) 

draw.text((0, 0), text, font=obj, fill=color) 

return video_image(img, **kwargs) 

 

 

def video_frame(fct_frame, **kwargs): 

""" 

Creates a video from drawing or images. 

*fct_frame* can either be a function which draws a picture at time *t* 

or a list of picture names or a folder. 

Créé une vidéo à partir de dessins ou d'images. 

*fct_frame* est soit une fonction qui dessine chaque image à chaque instant *t*, 

une liste de noms d'images ou un répertoire. 

 

@param fct_frame function like ``def make_frame(t: float) -> numpy.ndarray``, 

or list of images or folder name 

@param kwargs additional arguments for function 

`make_frame <https://zulko.github.io/moviepy/getting_started/videoclips.html#videoclip>`_ 

@return :epkg:`VideoClip` 

""" 

if isinstance(fct_frame, str): 

if not os.path.exists(fct_frame): 

raise FileNotFoundError( 

"Unable to find folder '{0}'".format(fct_frame)) 

imgs = os.listdir(fct_frame) 

exts = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff'} 

imgs = [os.path.join(fct_frame, _) 

for _ in imgs if os.path.splitext(_)[-1].lower() in exts] 

return video_frame(imgs, **kwargs) 

elif isinstance(fct_frame, list): 

for img in fct_frame: 

if not os.path.exists(img): 

raise FileNotFoundError( 

"Unable to find image '{0}'".format(img)) 

return ImageSequenceClip(fct_frame, **kwargs) 

else: 

return VideoClip(fct_frame, **kwargs)