• ffmpeg-go库的介绍


    ffmpeg-go is a Go programming language wrapper for the FFmpeg multimedia framework. It allows developers to easily integrate FFmpeg functionalities into their Go applications, such as decoding and encoding audio and video files, converting media formats, and applying various filters and effects. FFmpeg-go provides a simple and intuitive API, making it easier for Go developers to utilize the powerful multimedia features of FFmpeg.

    本质上ffmpeg-go是对命令行ffmpeg工具的封装,最终功能的实现都是通过在go里执行命令行工具来实现。

    我们来看个例子。

    在go代码中调用ffprobe命令行工具。

    1. // 包装ffprobe工具
    2. // Probe Run ffprobe on the specified file and return a JSON representation of the output.
    3. func Probe(fileName string, kwargs ...KwArgs) (string, error) {
    4. return ProbeWithTimeout(fileName, 0, MergeKwArgs(kwargs))
    5. }
    6. // 为命令行执行ffprobe增加timeout
    7. func ProbeWithTimeout(fileName string, timeOut time.Duration, kwargs KwArgs) (string, error) {
    8. args := KwArgs{
    9. "show_format": "",
    10. "show_streams": "",
    11. "of": "json",
    12. }
    13. return ProbeWithTimeoutExec(fileName, timeOut, MergeKwArgs([]KwArgs{args, kwargs}))
    14. }
    15. // 生成ffprobe命令行执行用到的参数
    16. func ProbeWithTimeoutExec(fileName string, timeOut time.Duration, kwargs KwArgs) (string, error) {
    17. args := ConvertKwargsToCmdLineArgs(kwargs)
    18. args = append(args, fileName)
    19. ctx := context.Background()
    20. // 将超时转换为context控制
    21. if timeOut > 0 {
    22. var cancel func()
    23. ctx, cancel = context.WithTimeout(context.Background(), timeOut)
    24. defer cancel()
    25. }
    26. cmd := exec.CommandContext(ctx, "ffprobe", args...)
    27. buf := bytes.NewBuffer(nil)
    28. stdErrBuf := bytes.NewBuffer(nil)
    29. cmd.Stdout = buf
    30. cmd.Stderr = stdErrBuf
    31. for _, option := range GlobalCommandOptions {
    32. option(cmd)
    33. }
    34. // 运行命令行的命令
    35. // 实际运行的是: ffprobe -of json -show_format -show_streams ${fileName}
    36. err := cmd.Run()
    37. if err != nil {
    38. return "", fmt.Errorf("[%s] %w", string(stdErrBuf.Bytes()), err)
    39. }
    40. return string(buf.Bytes()), nil
    41. }

    输出示例:

    1. "streams": [
    2. {
    3. "index": 0,
    4. "codec_name": "h264",
    5. "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
    6. "profile": "Main",
    7. "codec_type": "video",
    8. "codec_tag_string": "avc1",
    9. "codec_tag": "0x31637661",
    10. "width": 320,
    11. "height": 240,
    12. "coded_width": 320,
    13. "coded_height": 240,
    14. "closed_captions": 0,
    15. "film_grain": 0,
    16. "has_b_frames": 0,
    17. "sample_aspect_ratio": "1:1",
    18. "display_aspect_ratio": "4:3",
    19. "pix_fmt": "yuv420p",
    20. "level": 13,
    21. "chroma_location": "left",
    22. "field_order": "progressive",
    23. "refs": 1,
    24. "is_avc": "true",
    25. "nal_length_size": "4",
    26. "id": "0x1",
    27. "r_frame_rate": "30000/1001",
    28. "avg_frame_rate": "30000/1001",
    29. "time_base": "1/90000",
    30. "start_pts": 0,
    31. "start_time": "0.000000",
    32. "duration_ts": 627627,
    33. "duration": "6.973633",
    34. "bit_rate": "251413",
    35. "bits_per_raw_sample": "8",
    36. "nb_frames": "209",
    37. "extradata_size": 37,
    38. "disposition": {
    39. "default": 1,
    40. "dub": 0,
    41. "original": 0,
    42. "comment": 0,
    43. "lyrics": 0,
    44. "karaoke": 0,
    45. "forced": 0,
    46. "hearing_impaired": 0,
    47. "visual_impaired": 0,
    48. "clean_effects": 0,
    49. "attached_pic": 0,
    50. "timed_thumbnails": 0,
    51. "captions": 0,
    52. "descriptions": 0,
    53. "metadata": 0,
    54. "dependent": 0,
    55. "still_image": 0
    56. },
    57. "tags": {
    58. "language": "und",
    59. "handler_name": "VideoHandler",
    60. "vendor_id": "[0][0][0][0]"
    61. }
    62. },
    63. {
    64. "index": 1,
    65. "codec_name": "aac",
    66. "codec_long_name": "AAC (Advanced Audio Coding)",
    67. "profile": "LC",
    68. "codec_type": "audio",
    69. "codec_tag_string": "mp4a",
    70. "codec_tag": "0x6134706d",
    71. "sample_fmt": "fltp",
    72. "sample_rate": "44100",
    73. "channels": 2,
    74. "channel_layout": "stereo",
    75. "bits_per_sample": 0,
    76. "initial_padding": 0,
    77. "id": "0x2",
    78. "r_frame_rate": "0/0",
    79. "avg_frame_rate": "0/0",
    80. "time_base": "1/44100",
    81. "start_pts": 0,
    82. "start_time": "0.000000",
    83. "duration_ts": 310272,
    84. "duration": "7.035646",
    85. "bit_rate": "125587",
    86. "nb_frames": "303",
    87. "extradata_size": 2,
    88. "disposition": {
    89. "default": 1,
    90. "dub": 0,
    91. "original": 0,
    92. "comment": 0,
    93. "lyrics": 0,
    94. "karaoke": 0,
    95. "forced": 0,
    96. "hearing_impaired": 0,
    97. "visual_impaired": 0,
    98. "clean_effects": 0,
    99. "attached_pic": 0,
    100. "timed_thumbnails": 0,
    101. "captions": 0,
    102. "descriptions": 0,
    103. "metadata": 0,
    104. "dependent": 0,
    105. "still_image": 0
    106. },
    107. "tags": {
    108. "language": "und",
    109. "handler_name": "SoundHandler",
    110. "vendor_id": "[0][0][0][0]"
    111. }
    112. }
    113. ],
    114. "format": {
    115. "filename": "./examples/sample_data/in1.mp4",
    116. "nb_streams": 2,
    117. "nb_programs": 0,
    118. "format_name": "mov,mp4,m4a,3gp,3g2,mj2",
    119. "format_long_name": "QuickTime / MOV",
    120. "start_time": "0.000000",
    121. "duration": "7.035646",
    122. "size": "336833",
    123. "bit_rate": "383001",
    124. "probe_score": 100,
    125. "tags": {
    126. "major_brand": "isom",
    127. "minor_version": "512",
    128. "compatible_brands": "isomiso2avc1mp41",
    129. "encoder": "Lavf57.71.100"
    130. }
    131. }
    132. }

  • 相关阅读:
    官方教程 Redshift 04 渲染参数
    玄 - 利用DLL通知回调函数注入shellcode(上)
    torch模块常用方法总结
    车载多媒体没法显示歌词的解决办法 —— 修改 LRC 文件的编码格式为 UTF-8
    Xcode打包ipa文件,查看app包内文件
    【hack】浅浅说说自己构造hack的一些逻辑~
    数据结构:二叉树及堆排序
    【数据结构】带头双向链表的简单实现
    CUDA/安装/win10
    【动态规划】Leetcode 279. 完全平方数【中等】
  • 原文地址:https://blog.csdn.net/hugo_lei/article/details/133849128