This file is indexed.

/usr/share/pyshared/scitools/easyviz/movie.py is in python-scitools 0.9.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
#!/usr/bin/env python

import os, glob, re

from scitools.misc import findprograms
from misc import _check_type

class MovieEncoder(object):
    """
    Class for turning a series of filenames with frames in a movie into
    a movie file.
    """
    _local_prop = {
        'input_files': None,         # image files
        'output_file': None,         # resulting movie
        'overwrite_output': True,    # overwrite output file if True
        'encoder': None,             # encoding tool
        'vbitrate': None,            # video bit rate
        'vbuffer': None,             # video buffer
        'fps': 25,                   # default frame rate
        'vcodec': 'mpeg4',           # default video codec
        'size': None,                # final movie size
        'quiet': False,              # run in quite mode
        'aspect': None,              # aspect ratio
        'preferred_package': 'ImageMagick',  # prefer ImageMagick or Netpbm
        'qscale': None,              # quantization scale
        'qmin': 2,                   # minimum quantization scale
        'qmax': 31,                  # maximum quantization scale
        'iqscale': None,             # quantization scale for I frames
        'pqscale': None,             # quantization scale for P frames
        'bqscale': None,             # quantization scale for B frames
        'pattern': 'I',              # pattern for I, P, and B frames
        'gop_size': None,            # size of GOP (group of pictures)
        'force_conversion': False,   # force conversion (to png) if True
        'cleanup': True,             # clean up temporary files
        }
    _legal_encoders = 'convert mencoder ffmpeg mpeg_encode ppmtompeg '\
                      'mpeg2enc html'.split()
    _legal_file_types = 'png gif jpg ps eps bmp tif tga pnm'.split()

    def __init__(self, input_files, **kwargs):
        self._prop = {}
        self._prop.update(self._local_prop)
        self._prop['input_files'] = input_files
        for key in kwargs:
            if key in self._prop:
                self._prop[key] = kwargs[key]

        print '\n\n' # provide some space before print statements

        # Determine which encoder to be used
        encoder = self._prop['encoder']
        if encoder is None:
            # No encoder given, find the first installed among the legal ones
            for enc in self._legal_encoders:
                if findprograms(enc):
                    encoder = enc
                    break
            if encoder is None:
                # No encoder is installed, fall back on html
                encoder = 'html'
            self._prop['encoder'] = encoder
        else:
            if not encoder in self._legal_encoders:
                raise ValueError("encoder must be %s, not '%s'" %
                                 (self._legal_encoders, encoder))
            if not encoder.startswith('html') and not findprograms(encoder):
                raise Exception("The selected encoder (%s) is not installed" \
                                % encoder)

        # Determine the file type of the input files
        if isinstance(input_files, (tuple,list)):
            file_ = input_files[0]
        elif isinstance(input_files, str):
            file_ = input_files
        else:
            raise ValueError("The input files must be given as either a "\
                             "list/tuple of strings or a string, not '%s'" % \
                             type(input_files))

        # Check that the input files do exist
        if isinstance(input_files, str):
            # Are input_files on ffmpeg/mpeg2enc format or Unix wildcard format?
            ffmpeg_format = r'(.+)%\d+d(\..+)'
            m = re.search(ffmpeg_format, input_files, re.DOTALL)
            if m:
                wildcard_format = m.group(1) + '*' + m.group(2)
            else:
                wildcard_format = input_files
            all_input_files = glob.glob(wildcard_format)
            if not all_input_files:
                print 'No files of the form %s exist.' % input_files
            else:
                print 'Found %d files of the format %s.' % \
                (len(all_input_files), input_files)
        else:  # list of specific filenames
            all_input_files = input_files
        error_encountered = False
        for f in all_input_files:
            if not os.path.isfile(f):
                print 'Input file %s does not exist.' % f
                error_encountered = True
        if error_encountered:
            raise IOError('Some input files were not found.')

        fname, ext = os.path.splitext(file_)
        if not ext:
            raise ValueError("Unable to determine file type from file name.")
        file_type = ext[1:] # remove the . (dot)
        if not file_type in self._legal_file_types:
            raise TypeError("File type must be %s, not '%s'" % \
                            (self._legal_file_types, file_type))
        self._prop['file_type'] = file_type

    def encode(self):
        """Encode a series of images to a movie."""
        encoder = self._prop['encoder']

        if encoder == 'html':
            # Don't make movie file, just an html file that can play png files
            files = self._prop['input_files']
            if isinstance(files, str):
                files = glob.glob(files)
                files.sort()
            print '\nMaking HTML code for displaying', ', '.join(files)
            fps = self._prop['fps']
            interval_ms = 1000.0/fps
            outf = self._prop['output_file']
            if outf is None:
                outf = 'movie.html'
            else:
                # Ensure .html extension
                outf = os.path.splitext(outf)[0] + '.html'
            casename = os.path.splitext(outf)[0]
            header, jscode, form, footer = \
                html_movie(files, interval_ms, casename=casename)

            f = open(outf, 'w')
            f.write(header + jscode + form + footer)
            f.close()
            print "\n\nmovie in output file", outf
            return

        # Get command string (all other encoders are run as stand-alone apps)
        exec('cmd=self._%s()' % encoder)

        # Run command
        if not self._prop['quiet']:
            print "\nscitools.easyviz.movie function runs the command: \n\n%s\n" % cmd
        failure = os.system(cmd)
        if failure:
            print '\n\nscitools.easyviz.movie could not make movie'
            raise SystemError('Check error messages from the encoder in the terminal window')
        elif not self._prop['quiet']:
            print "\n\nmovie in output file", self._prop['output_file']

        # Clean up temporary files
        if self._prop['cleanup'] and hasattr(self, '_tmp_files'):
            for tmp_file in self._tmp_files:
                os.remove(tmp_file)

    def _convert(self):
        """Return a string with commands for making a movie with the convert
        tool (from ImageMagick)."""
        encoder = self._prop['encoder']
        cmd = encoder

        # set number of frames per second:
        #cmd += ' -delay 1x%s' % self._prop['fps']
        cmd += ' -delay %d' % (1.0/self._prop['fps']*100)

        # set size:
        size = self._get_size()
        if size is not None:
            cmd += ' -scale %sx%s' % (size[0], size[1])

        # get image files:
        files = self._prop['input_files']
        if isinstance(files, str):
            pattern = r'(.*)%(\d+)d(.*\..*)'
            match = re.search(pattern, files)
            if match:
                pre = match.group(1)
                num = int(match.group(2))
                ext = match.group(3)
                files = pre + '[0-9]'*num + ext
            files = glob.glob(files)
            files.sort()
        if not files:
            raise ValueError(
                "'%s' is not a valid file specification or the files " \
                "does not exist." % files)
        if isinstance(self._prop['input_files'], str):
            cmd += ' ' + self._prop['input_files']
        else:
            cmd += ' %s' % (' '.join(files))

        # set output file:
        if self._prop['output_file'] is None:
            self._prop['output_file'] = 'movie.gif'
        output_file = self._prop['output_file']
        if output_file[-4:] != '.gif':
            output_file += '.gif'
        if os.path.isfile(output_file) and not self._prop['overwrite_output']:
            raise Exception("Output file '%s' already exist. Use" \
                            " 'overwrite_output=True' to overwrite the file." \
                            % output_file)
        cmd += ' %s' % output_file

        return cmd

    def _mencoder(self):
        """Return a string with commands for making a movie with the MEncoder
        tool.
        """
        encoder = self._prop['encoder']
        cmd = encoder
        file_type = self._prop['file_type']
        files = self._prop['input_files']
        if isinstance(files, str):
            pattern = r'(.*)%(\d+)d(.*\..*)'
            match = re.search(pattern, files)
            if match:
                pre = match.group(1)
                num = int(match.group(2))
                ext = match.group(3)
                files = pre + '[0-9]'*num + ext
            files = glob.glob(files)
            files.sort()
            if not files:
                raise ValueError(
                    "'%s' is not a valid file specification or the files "\
                    "does not exist." % self._prop['input_files'])
        if isinstance(files, (list,tuple)):
            if not file_type in ['jpg', 'png'] or \
                   self._prop['force_conversion']:
                # since mencoder only supports jpg and png files, we have to
                # create copies of the input files and convert them to jpg.
                files = self._any2any(files, ofile_ext='.png')
                file_type = 'png'
                self._tmp_files = files[:] # store files for later removal
            # give the files as a comma separated string to mencoder:
            files = ','.join(files)
        cmd += ' "mf://%s" -mf' % files
        cmd += ' fps=%g' % float(self._prop['fps'])
        cmd += ':type=%s' % file_type
        vbitrate = self._prop['vbitrate']
        if vbitrate is None:
            vbitrate = 800
        vcodec = self._prop['vcodec']
        qscale = self._prop['qscale']
        qmin = self._prop['qmin']
        qmax = self._prop['qmax']
        vbuffer = self._prop['vbuffer']
        if vcodec == 'xvid':
            cmd += ' -ovc xvid -xvidencopts'
            if qscale is not None:
                cmd += ' fixed_quant=%s' % qscale
            else:
                cmd += ' bitrate=%s:' % vbitrate
                cmd += 'min_iquant=%s:max_iquant=%s:' % (qmin, qmax)
                cmd += 'min_pquant=%s:max_pquant=%s:' % (qmin, qmax)
                cmd += 'min_bquant=%s:max_bquant=%s' % (qmin, qmax)
        else:
            # mbd: macroblock decision
            cmd += ' -ovc lavc -lavcopts vcodec=%s:mbd=1' % vcodec
            if vbitrate is not None:
                cmd += ':vbitrate=%s' % vbitrate
                #cmd += ':vrc_minrate=%s:vrc_maxrate=%s' % ((vbitrate,)*2)
            if qscale is not None:
                cmd += ':vqscale=%s' % qscale
            else:
                cmd += ':vqmin=%s:vqmax=%s' % (qmin, qmax)
            if vbuffer is not None:
                cmd += ':vrc_buf_size=%s' % vbuffer
        aspect = self._prop['aspect']
        if aspect is not None:
            cmd += ':aspect=%s' % aspect
        #cmd += ' -oac copy' # audio
        size = self._get_size()
        if size is not None:
            cmd += ' -vf scale=%s:%s' % (size[0], size[1])
        # if no output file is given, use 'movie.avi' as default:
        if self._prop['output_file'] is None:
            self._prop['output_file'] = 'movie.avi'
        output_file = self._prop['output_file']
        if os.path.isfile(output_file) and not self._prop['overwrite_output']:
            raise Exception("Output file '%s' already exist. Use" \
                            " 'overwrite_output=True' to overwrite the file." \
                            % output_file)
        cmd += ' -o %s' % output_file
        if self._prop['quiet']:
            cmd += ' > /dev/null 2>&1'
        return cmd

    def _ffmpeg(self):
        """Return a string with commands for making a movie with the FFmpeg
        tool.
        """
        encoder = self._prop['encoder']
        cmd = encoder
        files = self._prop['input_files']
        file_type = self._prop['file_type']
        if isinstance(files, str):
            pattern = r'(.*)%(\d+)d(.*\..*)'
            match = re.search(pattern, files)
            if match:
                if file_type not in ['jpg', 'png'] or \
                       self._prop['force_conversion']:
                    pre = match.group(1)
                    num = int(match.group(2))
                    ext = match.group(3)
                    files = pre + '[0-9]'*num + ext
                    files = glob.glob(files)
                    files.sort()
            else:
                files = glob.glob(files)
                files.sort()
        if isinstance(files, (list,tuple)):
            basename = 'easyviz_tmp_'
            files = self._any2any(files, basename=basename, ofile_ext='.png')
            file_type = 'png'
            self._tmp_files = files[:]
            # create a new string with the right pattern:
            files = basename + '%04d.png'
        cmd += ' -i "%s"' % files
        vbitrate = self._prop['vbitrate']
        if vbitrate is None:
            vbitrate = "800k"
        cmd += ' -b %s' % vbitrate
        cmd += ' -r %s' % self._prop['fps']
        size = self._get_size()
        if size is not None:
            cmd += ' -s %sx%s' % size
        if self._prop['vcodec'] is not None:
            cmd += ' -vcodec %s' % self._prop['vcodec']
        if self._prop['overwrite_output']:
            cmd += ' -y'
        if self._prop['aspect'] is not None:
            cmd += ' -aspect %s' % self._prop['aspect']
        if self._prop['qscale'] is not None:
            cmd += ' -qscale %s' % self._prop['qscale']
        else:
            cmd += ' -qmin %s -qmax %s' % \
                   (self._prop['qmin'], self._prop['qmax'])
        if self._prop['vbuffer'] is not None:
            cmd += ' -bufsize %s' % self._prop['vbuffer']
        if self._prop['gop_size'] is not None:
            cmd += ' -g %d' % int(self._prop['gop_size'])
        #cmd += ' -target dvd'
        # if no output file is given, use 'movie.avi' as default:
        if self._prop['output_file'] is None:
            self._prop['output_file'] = 'movie.avi'
        cmd += ' ' + self._prop['output_file']
        if self._prop['quiet']:
            cmd += ' > /dev/null 2>&1'
        return cmd

    def _mpeg_encode(self):
        """Return a string with commands for making a movie with the
        mpeg_encode tool.
        """
        encoder = self._prop['encoder']
        basename = 'easyviz_tmp_'  # basename for temporary files

        # set frame rate:
        # mpeg_encode only supports a given set of frame rates:
        legal_frame_rates = (23.976, 24, 25, 29.97, 30, 50, 59.94, 60)
        if self._prop['fps'] in legal_frame_rates:
            fps = self._prop['fps']
        else:
            raise ValueError("%s only supports the these frame rates: %s" % \
                             (encoder, legal_frame_rates))

        # set aspect ratio:
        legal_aspects = (1.0, 0.6735, 0.7031, 0.7615, 0.8055,
                         0.8437, 0.8935, 0.9157, 0.9815, 1.0255,
                         1.0695, 1.0950, 1.1575, 1.2015)
        aspect = self._prop['aspect']
        if aspect is not None:
            if aspect not in legal_aspects:
                raise(ValueError, \
                      "%s only supports the following aspect ratios: %s" % \
                      (encoder, legal_aspects))
        else:
            aspect = 1.0
        print aspect

        # get image files:
        files = self._prop['input_files']
        if isinstance(files, str):
            pattern = r'(.*)%(\d+)d(.*\..*)'
            match = re.search(pattern, files)
            if match:
                pre = match.group(1)
                num = int(match.group(2))
                ext = match.group(3)
                files = pre + '[0-9]'*num + ext
            files = glob.glob(files)
            files.sort()
        if not files:
            raise ValueError(
                "'%s' is not a valid file specification or the files " \
                "does not exist." % files)
        size = self._get_size()
        if size is not None or self._prop['file_type'] != 'pnm' or \
               self._prop['force_conversion']:
            files = self._any2any(files, basename=basename, size=size)
            self._tmp_files = files[:]
        files = '\n'.join(files)

        # set input dir:
        input_dir = os.path.split(files[0])[0]
        if input_dir == '':
            input_dir = '.'

        # set output file:
        if self._prop['output_file'] is None:
            self._prop['output_file'] = 'movie.mpeg'
        mpeg_file = self._prop['output_file']
        if os.path.isfile(mpeg_file) and not self._prop['overwrite_output']:
            raise Exception("Output file '%s' already exist. Use" \
                            " 'overwrite_output=True' to overwrite the file." \
                            % mpeg_file)

        # set pattern (sequence of I, P, and B frames):
        pattern = self._prop['pattern']
        if isinstance(pattern, str):
            pattern = pattern.upper()  # pattern must be uppercase
        else:
            # give warning?
            pattern = 'I'

        # set quantization scale for I, P, and B frames:
        qscale = self._prop['qscale']
        iqscale = self._prop['iqscale']
        pqscale = self._prop['pqscale']
        bqscale = self._prop['bqscale']
        if qscale is not None:
            if iqscale is None: iqscale = qscale
            if pqscale is None: pqscale = qscale
            if bqscale is None: bqscale = qscale
        else:
            if iqscale is None: iqscale = 8
            if pqscale is None: pqscale = 10
            if bqscale is None: bqscale = 25

        # set gop size:
        gop_size = self._prop['gop_size']
        if gop_size is not None:
            gop_size = int(gop_size)
        else:
            gop_size = 15

        # create an mpeg_encode parameter file:
        mpeg_encode_file = "%s.mpeg_encode-input" % basename
        f = open(mpeg_encode_file, "w")
        f.write("""
PATTERN	         %(pattern)s
OUTPUT           %(mpeg_file)s
BASE_FILE_FORMAT PNM
INPUT_CONVERT    *
GOP_SIZE         %(gop_size)d
#GOP_SIZE         16
SLICES_PER_FRAME 1
INPUT_DIR        %(input_dir)s
INPUT
%(files)s
END_INPUT
PIXEL            FULL
#PIXEL            HALF
RANGE            10
PSEARCH_ALG      LOGARITHMIC
BSEARCH_ALG      CROSS2
IQSCALE	         %(iqscale)d
PQSCALE	         %(pqscale)d
BQSCALE	         %(bqscale)d
REFERENCE_FRAME  ORIGINAL
FRAME_RATE       %(fps)d
ASPECT_RATIO     %(aspect)s
FORCE_ENCODE_LAST_FRAME
""" % vars())

        # set video bit rate and buffer size:
        vbitrate = self._prop['vbitrate']
        if isinstance(vbitrate, (float,int)):
            f.write("BIT_RATE         %d" % (int(vbitrate)*1000))
        vbuffer = self._prop['vbuffer']
        if isinstance(vbuffer, (float,int)):
            f.write("BUFFER_SIZE      %d" % (int(vbuffer)*1000))
        f.close()

        if not hasattr(self, '_tmp_files'):
            self._tmp_files = []
        self._tmp_files.append(mpeg_encode_file)

        # create the command string:
        cmd = encoder
        if self._prop['quiet']:
            cmd += ' -realquiet'
        cmd += ' ' + mpeg_encode_file
        return cmd

    # mpeg_encode and ppmtompeg is the same encoding tool:
    _ppmtompeg = _mpeg_encode

    def _mpeg2enc(self):
        """Return a string with commands for making a movie with the
        mpeg2enc tool (from MJPEGTools).
        """
        encoder = self._prop['encoder']
        png2yuv = 'png2yuv'
        jpeg2yuv = 'jpeg2yuv'
        yuvscaler = 'yuvscaler'

        file_type = self._prop['file_type']
        files = self._prop['input_files']
        if isinstance(files, str):
            pattern = r'(.*)%(\d+)d(.*\..*)'
            match = re.search(pattern, files)
            if match:
                if file_type not in ['jpg', 'png'] or \
                       self._prop['force_conversion']:
                    pre = match.group(1)
                    num = int(match.group(2))
                    ext = match.group(3)
                    files = pre + '[0-9]'*num + ext
                    files = glob.glob(files)
                    files.sort()
            else:
                files = glob.glob(files)
                files.sort()
        if isinstance(files, (list,tuple)):
            basename = 'easyviz_tmp_'
            files = self._any2any(files, basename=basename, ofile_ext='.png')
            file_type = 'png'
            self._tmp_files = files[:]
            # create a new string with the right pattern:
            files = basename + '%04d.png'

        cmd = ''
        if file_type == 'jpg' and findprograms(jpeg2yuv):
            cmd += jpeg2yuv
        elif findprograms(png2yuv):
            cmd += png2yuv
        else:
            raise Exception("png2yuv or jpeg2yuv is not installed")
        cmd += ' -f 25' # frame rate
        cmd += ' -I p'  # interlacing mode: p = none / progressive
        cmd += ' -j "%s"' % files # set image files
        # find start image:
        for i in xrange(9999):
            if os.path.isfile(files % i):
                cmd += ' -b %d' % i
                break
        if self._prop['quiet']:
            cmd += ' -v 0' # verbosity level 0

        # set size of movie (by using the yuvscaler tool):
        size = self._get_size()
        if size is not None and findprograms(yuvscaler):
            width, height = size
            cmd += ' | %(yuvscaler)s -O SIZE_%(width)sx%(height)s' % vars()

        # if no output file is given, use 'movie.avi' as default:
        if self._prop['output_file'] is None:
            self._prop['output_file'] = 'movie.mpeg'
        output_file = self._prop['output_file']
        if os.path.isfile(output_file) and not self._prop['overwrite_output']:
            raise Exception("Output file '%s' already exist. Use" \
                            " 'overwrite_output=True' to overwrite the file." \
                            % output_file)

        cmd += ' | '
        cmd += encoder
        if self._prop['vcodec'] == 'mpeg2video':
            cmd += ' -f 3' # generic mpeg-2 video
        else:
            cmd += ' -f 0' # generic mpeg-1 video
        if self._prop['vbitrate'] is not None:
            cmd += ' -b %d' % int(self._prop['vbitrate'])
        if self._prop['vbuffer'] is not None:
            cmd += ' -V %d' % int(self._prop['vbuffer'])
        if self._prop['qscale'] is not None:
            cmd += ' -q %s' % self._prop['qscale']

        # set movie frame rate:
        legal_fps = {'23.976': 1, '24': 2, '25': 3, '29.97': 4,
                     '30': 5, '50': 6, '59.94': 7, '60': 8}
        fps = str(self._prop['fps'])
        if not fps in legal_fps:
            raise ValueError("fps must be %s, not %s" % \
                             (fps_convert.keys(), fps))
        cmd += ' -F %s' % legal_fps[fps]
        #cmd += ' --cbr' # constant bit rate
        gop_size = self._prop['gop_size']
        if gop_size is not None:
            # set min (-g) and max (-G) gop size to the same value:
            cmd += ' -g %s -G %s' % (gop_size, gop_size)

        # set aspect ratio:
        legal_aspects = {'1.0': 1, '1.3': 2, '1.7': 3, '2.21': 4}
        aspect = self._get_aspect_ratio()
        if aspect is not None:
            if aspect not in legal_aspects.values():
                aspect = str(aspect)
                for key in legal_aspects.keys():
                    if aspect.startswith(key):
                        aspect = legal_aspects[key]
                        break
                if aspect not in legal_aspects.values():
                    raise ValueError(
                        "aspect must be either 1:1, 4:3, 16:9, or 2.21:1," \
                        " not '%s'" % aspect)
            cmd += ' -a %s' % aspect

        # set output file:
        cmd += ' -o %s' % self._prop['output_file']
        if self._prop['quiet']:
            cmd += ' -v 0' # verbosity level 0 (warnings and errors only)
        return cmd

    def _any2any(self, files, basename='easyviz_tmp_',
                 size=None, ofile_ext='.pnm'):
        """Convert a list of files to the file format specified in the
        ofile_ext keyword argument. Using either Netpbm tools or convert
        (from the ImageMagick package).
        """
        netpbm_converters = {'.png': ('pngtopnm', 'pnmtopng'),
                             '.gif': ('giftopnm',  'ppmtogif'),
                             '.jpg': ('jpegtopnm', 'pnmtojpeg'),
                             '.ps':  ('pstopnm', 'pnmtops'),
                             '.eps': ('pstopnm', 'pnmtops'),
                             '.bmp': ('bmptopnm', 'ppmtobmp'),
                             '.tif': ('tifftopnm', 'pnmtotiff'),
                             '.tga': ('tgatopnm', 'ppmtotga'),
                             '.pnm': ('cat', ''),
                             }
        _check_type(files, 'files', (list,tuple))
        ifile_ext = os.path.splitext(files[0])[1]
        anytopnm = netpbm_converters[ifile_ext][0]
        pnmtoany = netpbm_converters[ofile_ext][1]
        pnmscale = 'pnmscale'
        #pnmcrop = 'pnmcrop'
        convert = 'convert'

        app = anytopnm
        if findprograms((convert, anytopnm, pnmtoany)):
            if self._prop['preferred_package'].lower() == 'imagemagick':
                app = convert
        elif findprograms(convert):
            app = convert
        elif not findprograms((anytopnm, pnmtoany)):
            raise Exception("Neither %s nor %s was found" % (convert,anytopnm))

        quiet = self._prop['quiet']
        new_files = []
        i = 1 # counter
        for file_ in files:
            new_file = "%s%04d%s" % (basename, i, ofile_ext)
            if app == anytopnm:
                options = ''
                if quiet and app != 'cat':
                    options += '-quiet'
                if app == 'pstopnm':
                    options += ' -stdout'
                    #options += ' -portrait'
                cmd = "%(app)s %(options)s %(file_)s " % vars()
                if size is not None and findprograms(pnmscale):
                    w, h = size
                    cmd += "| %(pnmscale)s -width %(w)s -height %(h)s" % vars()
                if pnmtoany != '':
                    options = ''
                    if quiet:
                        options += '-quiet'
                    if pnmtoany == 'pnmtojpeg':
                        options += ' -quality 100' # don't lose quality
                    cmd += " | %(pnmtoany)s %(options)s" % vars()
                cmd += " > %s" % new_file
            else:
                options = ''
                if size is not None:
                    options += '-resize %sx%s' % size
                cmd = "%(app)s %(options)s %(file_)s %(new_file)s" % vars()
            if not quiet:
                print cmd
            failure = os.system(cmd)
            if failure:
                print "... %s failed, jumping to next file..." % app
                continue
            new_files.append(new_file)
            if not quiet:
                apps = app
                if app != convert and pnmtoany != '':
                    apps += ' and %s' % pnmtoany
                print "%s transformed via %s to %s (%d Kb)" % \
                      (file_,apps,new_file,int(os.path.getsize(new_file)/1000))
            i += 1

        return new_files

    def _get_aspect_ratio(self):
        """Parse and return the aspect ratio."""
        # accept aspect ratio on the form 4:3, 4/3, or 1.3333
        aspect = self._prop['aspect']
        if isinstance(aspect, str):
            if aspect.find(':') > 0:
                aspect = aspect.split(':')
            elif aspect.find('/'):
                aspect = aspect.split('/')
            else:
                try: aspect = float(aspect)
                except: aspect = None
            try: aspect = float(aspect[0]) / float(aspect[1])
            except: aspect = None
        return aspect

    def _get_size(self):
        """Parse and return the size."""
        legal_sizes = {'sqcif': (128, 96),
                       'qcif': (176, 144),
                       'cif': (352, 288),
                       '4cif': (704, 576)}
        size = self._prop['size']
        if isinstance(size, str):
            if size in legal_sizes:
                size = legal_sizes[size]
            else:
                size = size.split('x') # wxh
        if not (isinstance(size, (tuple,list)) and len(size) == 2):
            size = None
        return size

def html_movie(plotfiles, interval_ms=300, width=800, height=600,
               casename='movie'):
    """
    Takes a list plotfiles which should be for example of the form:
        ['frame00.png', 'frame01.png', ... ]
    where each string should be the name of an image file and they should be
    in the proper order for viewing as an animation.

    The result is html text strings that incorporate javascript to
    loop through the plots one after another.  The html text also features
    buttons for controlling the movie.
    The parameter iterval_ms is the time interval between loading
    successive images and is in milliseconds.

    The width and height parameters do not seem to have any effect
    for reasons not understood.

    The following strings are returned: header, javascript code, form
    with movie and buttons, and footer. Concatenating these strings
    and dumping to an html file yields a kind of movie file to be
    viewed in a browser. The images variable in the javascript code
    is unique for each movie, because it is annotated by the casename
    string, so several such javascript sections can be used in the
    same html file.

    This function is based on code written by R.J. LeVeque, based on
    a template from Alan McIntyre.
    """
    import os
    if not isinstance(plotfiles, (tuple,list)):
        raise TypeError('html_movie: plotfiles=%s of wrong type %s' %
                        (str(plotfiles), type(plotfiles)))
    # Check that the plot files really exist
    missing_files = [fname for fname in plotfiles if not os.path.isfile(fname)]
    if missing_files:
        raise ValueError('Missing plot files: %s' % str(missing_files)[1:-1])

    ext = os.path.splitext(plotfiles[0])[-1]
    if ext == '.png' or ext == '.jpg' or ext == '.jpeg' or ext == 'gif':
        pass
    else:
        raise ValueError('Plotfiles (%s, ...) must be PNG files with '\
                         'extension .png' % plotfiles[0])
    header = """\
<html>
<head>
</head>
<body>
"""
    no_images = len(plotfiles)
    jscode = """
<script language="Javascript">
<!---
var num_images_%(casename)s = %(no_images)d;
var img_width = %(width)d;
var img_height = %(height)d;
var interval = %(interval_ms)d;
var images_%(casename)s = new Array();

function preload_images_%(casename)s()
{
   t = document.getElementById("progress");
""" % vars()

    i = 0
    for fname in plotfiles:
        jscode += """
   t.innerHTML = "Preloading image ";
   images_%(casename)s[%(i)s] = new Image(img_width, img_height);
   images_%(casename)s[%(i)s].src = "%(fname)s";
        """ % vars()
        i = i+1
    jscode += """
   t.innerHTML = "";
}

function tick_%(casename)s()
{
   if (frame_%(casename)s > num_images_%(casename)s - 1)
       frame_%(casename)s = 0;

   document.movie.src = images_%(casename)s[frame_%(casename)s].src;
   frame_%(casename)s += 1;
   tt = setTimeout("tick_%(casename)s()", interval);
}

function startup_%(casename)s()
{
   preload_images_%(casename)s();
   frame_%(casename)s = 0;
   setTimeout("tick_%(casename)s()", interval);
}

function stopit()
{ clearTimeout(tt); }

function restart_%(casename)s()
{ tt = setTimeout("tick_%(casename)s()", interval); }

function slower()
{ interval = interval/0.7; }

function faster()
{ interval = interval*0.7; }

// --->
</script>
""" % vars()
    plotfile0 = plotfiles[0]
    form = """
<form>
&nbsp;
<input type="button" value="Start movie" onClick="startup_%(casename)s()">
<input type="button" value="Pause movie" onClick="stopit()">
<input type="button" value="Restart movie" onClick="restart_%(casename)s()">
&nbsp;
<input type="button" value="Slower" onClick="slower()">
<input type="button" value="Faster" onClick="faster()">
</form>

<p><div ID="progress"></div></p>
<img src="%(plotfile0)s" name="movie" border=2/>
""" % vars()
    footer = '\n</body>\n</html>\n'
    return header, jscode, form, footer


def movie(input_files, **kwargs):
    """
    Make a movie from a series of image files.

    This function makes it very easy to create a movie file from a
    series of image files. Several different encoding tools can be
    used, such as an HTML file, MEncoder, FFmpeg, mpeg_encode,
    ppmtompeg (Netpbm), mpeg2enc (MJPEGTools), and convert (ImageMagick),
    to combine the image files together. The encoding tool will be chosen
    automatically among these if more than one is installed on the
    machine in question (unless it is specified as a keyword argument
    to the movie function).

    Suppose we have some image files named `image_0000.eps`, `image_0001.eps`,
    `image_0002.eps`, ... Note that the zero-padding, obtained by the printf
    format `04d` in this case, ensures that the files are listed in correct
    numeric order when using a wildcard notation like `image_*.eps`.
    We want to make a movie out of these files, where each file constitutes
    a frame in the movie. This task can be accomplished by the simple call::

        movie('image_*.eps')

    The result is a movie file with a default name such as `movie.html`,
    `movie.avi`, `movie.mpeg`, or `movie.gif`, depending on the
    encoding tool chosen by the movie function. The file resides in
    the current working directory. The movie function checks a list of
    encoders and chooses the first it finds installed on the computer.

    Note: We strongly recommend to always clean up previously generated
    files for the frames in movies::

        for f in glob.glob('image_*.eps'):
            os.remove(f)

    Otherwise, there is a danger of mixing old and new files in the movie!

    One can easily specify the name of the movie file and explicitly
    specify the encoder. For example, an animated GIF movie can be
    created by::

        movie('image_*.eps', encoder='convert',
              output_file='../wave2D.gif')

    The encoder here is the convert program from the ImageMagick suite
    of image manipulation tools. The resulting movie file will be
    named 'wave2D.gif' and placed in the parent directory.

    Another convenient encoder is simply to make an HTML file that can
    play a series of image files::

        movie('image_*.png', encoder='html',
              output_file='../wave2D.html')

    Just load the output file into a web browser and play the movie.

    If you want to create an MPEG movie by using the MEncoder
    tool, you can do this with the following command::

        movie('image_*.eps', encoder='mencoder',
              output_file='/home/johannr/wave2D.mpeg',
              vcodec='mpeg2video', vbitrate=2400, qscale=4, fps=10)

    Here, we have also specified the video codec to be mpeg2video, the video
    bitrate to be 2400 kbps, the quantization scale (quality) to be 4, and
    the number of frames per second to be 10.

    Demo programs showing various use of the movie function can be
    found as the files movie_demo*.py files in the examples directory
    of the scitools source code tree.

    Suitable movie players are vlc for MPEG and AVI formats, and
    animate for animated GIF files.

    Below follows a more detailed description of the various arguments that
    are available in this function.

    Required arguments:

    input_files: Specifies the image files which will be used to make the
    movie. The argument must be given either as a string,
    e.g., 'image_*.png' or a list/tuple of strings, e.g.,
    glob.glob('image_*.png').

    Notes:

        * When using the FFmpeg or the Mpeg2enc tools, the image
          files should be given (if possible) as a string on the
          format '{1}%{2}d{3}', where the name components are as
          follows:

          - {1} filename prefix (e.g. image_)
          - {2} counting placeholder (like in C, printf, e.g. 04)
          - {3} file extension (e.g. .png or .jpg)

          Example of a correct description of the input files
          is image_%04d.png. If the input files are not given on
          the correct format, there will automatically be made
          copies of these files which will then be renamed to the
          required filename format.

        * MEncoder, FFmpeg, and Mpeg2enc supports only .jpg and
          .png image files. So, if the input files are on another
          format, there will automatically be made copies which
          in turn will be converted to the correct format.

    Optional arguments:

    output_file: Sets the name of the output movie. If not set, a default
    name like movie.avi, movie.mpeg, or movie.gif (depending
    on the output format) will be given.

    Note: When using the convert tool to combine the images,
    the extension of the file name is used to determine the
    file format of the final movie. For example, if a name like
    movie.gif is given, the resulting movie will become an
    animated gif file. Other supported movie formats are MPEG
    (.mpg, .mpeg, or .m2v) and MNG (Multiple-image Network
    Graphics).

    overwrite_output: If True, the file given in the output_file
    argument above will be overwritten without warning (if it already
    exists). The default is True.

    encoder: Sets the encoder tool to be used. Currently the following
    encoders are supported: 'html', 'mencoder', 'ffmpeg',
    'mpeg_encode', 'ppmtompeg' (from the Netpbm package),
    'mpeg2enc' (from the MJPEGTools package), and 'convert'
    (from the ImageMagick package).
    Note: 'ppmtompeg' and 'mpeg_encode' is the same tool.

    vbitrate: Sets the bit rate of the movie. The default is 800 kbps
    when using the FFmpeg and MEncoder encoders. For
    mpeg_encode, ppmtompeg, and mpeg2enc, this option is by
    default not specified. This option has no effect on the
    convert tool from ImageMagick.

    vbuffer: Sets the video buffer size. The default is to use the
    current encoding tools default video buffer size. In some
    cases it might be necessary to push this up to 500K or
    more.

    fps: Sets the number of frames per second for the final movie.
    The default is 25 fps.

    Notes:

     * The 'mpeg_encode', 'ppmtompeg', and 'mpeg2enc' tools only
       supports the following frame rates: 23.976, 24, 25,
       29.97, 30, 50, 59.94, and 60 fps.

     * Not all video codecs have support for arbitrary frame
       rates (e.g., 'mpeg1video' and 'mpeg2video').

    vcodec: Sets the video codec to be used. Some of the possible codecs
    are:

      - 'mjpeg'      - Motion JPEG
      - 'ljpeg'      - Lossless JPEG
      - 'h263'       - H.263
      - 'h263p'      - H.263+
      - 'mpeg4'      - MPEG-4 (DivX 4/5)
      - 'msmpeg4'    - DivX 3
      - 'msmpeg4v2'  - DivX 3
      - 'msmpeg4v2'  - MS MPEG4v2
      - 'wmv1'       - Windows Media Video, version 1 (AKA WMV7)
      - 'wmv2'       - Windows Media Video, version 2 (AKA WMV8)
      - 'rv10'       - an old RealVideo codec
      - 'mpeg1video' - MPEG-1 video
      - 'mpeg2video' - MPEG-2 video
      - 'huffyuv'    - HuffYUV
      - 'ffvhuff'    - nonstandard 20% smaller HuffYUV using YV12
      - 'asv1'       - ASUS Video v1
      - 'asv2'       - ASUS Video v2
      - 'ffv1'       - FFmpeg's lossless video codec

    The default codec is 'mpeg4' for mencoder/ffmpeg and
    'mpeg1video' for mpeg2enc.

    Notes:

      * Run 'ffmpeg -formats' for a longer list of available
        codecs.

      * The mencoder tool can also use the 'xvid' codec.

      * Only 'mpeg1video' and 'mpeg2video' are available when
        using the mpeg2enc tool.

      * This option has no effect when using mpeg_encode,
        ppmtompeg, or convert as the encoding tool.

    qscale: The quantization scale value (qscale) give a trade-off
    between quality and compression. A lower value means better
    quality but larger files. Larger values gives better
    compression, but worse quality. The qscale value must be an
    integer between 1 and 31. The default is to not set the
    qscale option.

    Note: When using mpeg_encode or ppmtompeg it is possible
    to have different qscale values for I, P, and B frames
    (see the iqscale, pqscale, and bqscale options below).

    qmin: Sets the minimum quantization scale value. Must be given as
    an integer between 1 and 31. The default is 2.

    qmax: Sets the maximum quantization scale value. Must be given as
    an integer between 1 and 31. The default is 31.

    iqscale: Sets the quantization scale value (see qscale) for I
    frames. This option only affects mpeg_encode and ppmtompeg.
    The default is to use the same value as in qscale. If
    qscale is not given, then 8 is the default value for
    iqscale.

    pqscale: Same as iqscale, but for P frames. If qscale is not given,
    then 10 is the default value for pqscale.

    bqscale: Same as iqscale, but for B frames. If qscale is not given,
    then 25 is the default value for bqscale.

    pattern: Sets the pattern (sequence) of I, P, and B frames. The
    default pattern is 'I' which gives good quality (but
    worse compression). Another standard sequence is
    'IBBPBBPBBPBBPBB'. This option has only an effect when
    using mpeg_encode or ppmtompeg as the encoding tool.

    size: Sets the size of the final movie. The size must be given
    as a tuple/list (e.g. (640,480)) or as a string. The
    format of the string must be 'wxh' (e.g. '320x240'), but
    the following abbreviations are also recognized:

      - 'sqcif' - 128x96
      - 'qcif'  - 176x144
      - 'cif'   - 352x288
      - '4cif'  - 704x576

    The default is to use the same size as the input images.

    aspect: Sets the aspect ratio of the movie (e.g. 4/3 or 16/9).

    Notes:

      * 'mpeg_encode' and 'ppmtompeg' only support the following
        aspect ratios: 1.0, 0.6735, 0.7031, 0.7615,0.8055,
        0.8437, 0.8935, 0.9157, 0.9815, 1.0255, 1.0695, 1.0950,
        1.1575, and 1.2015.

      * 'mpeg2enc' only supports the following aspect ratios: 1.0,
        1.33, 1.77, and 2.21.

    preferred_package: Sets whether to prefer the Netpbm package or the
    ImageMagick package if both of them are installed. Must be
    given as a string, i.e, either 'imagemagick' (default) or
    'netpbm'.

    Notes:

      * If only one of the packages is installed, then that
        package will be used.

      * If none of the packages are installed, then some
        operations might stop in lack of needed programs.

    gop_size: Sets the number of frames in a group of pictures (GOP).
    The default is to use the chosen encoding tools default
    value.

    quiet: If True, then the operations will run quietly and only
    complain on errors. The default is False.

    cleanup: If True (default), all temporary files that are created
    during the execution of the movie command will be deleted.

    force_conversion: Forces conversion of images. This is a hack that can
    be used if the encoding tool has problems reading the input
    image files. If this is set to True, the images will be
    converted even if they are in a format recognized by the
    encoding tool. The default is False.

    Known issues:

      * JPEG images created by the Vtk backend does not seem to work with
        the MEncoder and FFmpeg tools. This can be fixed by setting the
        force_conversion argument to True. This will force conversion of the
        JPEG files to PNG files which in turn should successfully create the
        movie.

      * Aspect ratio in mpeg_encode does not seem to work.
    """
    me = MovieEncoder(input_files, **kwargs)
    me.encode()


if __name__ == '__main__':
    pass