Javicle - a JSON Video Composition Language
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

jvc_js.md 3.2 KiB

hace 4 años
hace 4 años
hace 4 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # JVC JavaScript Expressions
  2. JavaScript expressions are used in a couple of places in JVC.
  3. * Operation configuration
  4. * Operation validations
  5. ## Operation Configuration
  6. When you're writing the JSON for an operation, many of the operation's
  7. config parameters can be JS expressions that will be evaluated
  8. when the operation is run.
  9. For example, say you wanted to use `trim` to just chop a video in half.
  10. The `start` and `end` parameters of the `trim` operation support JavaScript
  11. expressions, so you could write:
  12. ```js
  13. {
  14. "comment": "cut video at midpoint",
  15. "operation": "trim",
  16. "creates": "chopped"
  17. "source": "some_vid",
  18. "end": "source.duration / 2"
  19. }
  20. ```
  21. or run:
  22. ```shell script
  23. jtrim some_vid.mp4 output_vid.mp4 0 "source.duration / 2"
  24. ```
  25. In the above, `source` is referring to the source asset, `some_vid`.
  26. When JVC loads an asset (like `some_vid`), or an operation creates a new asset,
  27. the asset metadata is read using `mediainfo`. The metadata is stored in an object
  28. that is then put into the JavaScript context using the variable name `source`.
  29. ## Operation Validations
  30. Once an operation completes, the validations run to ensure that the output
  31. assets pass whatever tests you want to run.
  32. Each test is a JavaScript expressions, which is evaluated to a boolean value.
  33. If the expression is true, the test passes. If false, the test fails and the
  34. `comment` associated with the failing test is printed.
  35. ## JavaScript Context
  36. What variables are available in the JS context? And what properties do they have?
  37. ### Assets
  38. Assets can be defined in the JVC's `assets` array, or as the output of an
  39. operation.
  40. Assets are referenced by their asset name. Learn more about
  41. [Asset References](asset_refs.md).
  42. In a JVC JavaScript context, assets are JS objects with some useful properties:
  43. #### `duration`
  44. Duration in seconds of the asset (audio or video).
  45. #### `width`
  46. Resolution width in pixes (video or image).
  47. #### `height`
  48. Resolution height in pixes (video or image).
  49. #### `aspectRatio`
  50. Ratio of width / height (video or image).
  51. #### `samplingRate`
  52. Sampling rate in Hz (audio).
  53. #### `tracks`
  54. An array of the tracks in a video. Only includes audio and video tracks.
  55. #### `audioTracks`
  56. An array of the audio tracks in a video.
  57. #### `videoTracks`
  58. An array of the video tracks in a video.
  59. #### `assets`
  60. If an asset is a list asset, this is an array of the sub-assets. Each sub-asset
  61. has the same properties described above.
  62. #### more properties
  63. The list of asset properties supported today is fairly limited.
  64. More properties will be exposed in the future.
  65. ### JS Functions
  66. The JavaScript environment that JVC sets up includes some useful built-in
  67. functions.
  68. #### `is_within`
  69. Check if a variable is "close" to another value by comparing against a delta.
  70. ```js
  71. x = 10.2
  72. is_within(x, 10, 1); // true
  73. is_within(x, 9.5, 0.5); // false
  74. is_within(x, 10.3, 0.1); // true
  75. ```
  76. #### `is_within_pct`
  77. Check if a variable is "close" to another value by percentage. The percentage
  78. argument is an integer where 1 == 1% and 100 == 100%
  79. ```js
  80. x = 10.2
  81. is_within_pct(x, 10, 2); // true
  82. is_within_pct(x, 9, 10); // false (9 + (10% of 9) only gets you to 9.9, x is 10.2)
  83. is_within_pct(x, 10.3, 10); // true
  84. ```