使用什么ffmpeg命令将无符号整数列表转换为音频文件? [英] What ffmpeg command to use to convert a list of unsigned integers into an audio file?

查看:67
本文介绍了使用什么ffmpeg命令将无符号整数列表转换为音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中包含由空格分隔的大约四万个整数的列表,每个整数的值在0到255之间.这是此文件:

I have a file that contains a list of about forty thousand integers that are space delimited, with each integer between the value of 0 and 255. It is this file here:

https://github.com/johnlai2004/sound-project/blob/master/integers.txt

如果将扬声器连接到ESP32分支板上,然后以24kHz的频率通过数模转换器运行此整数列表,您会听到一句话,那不是您错过的帖子".

If you connect a speaker to an ESP32 breakout board, then run this list of integers through the digital to analog converter at a frequency of 24kHz, you will hear the sentence, "That's not the post that you missed."

我想知道的是如何使用FFMPEG将整数列表转换为其他计算机可以播放以听到相同短语的声音文件?我尝试了以下命令:

What I want to know is how do you use FFMPEG to convert this list of integers into a sound file that other computer can play to hear the same phrase? I tried this command:

ffmpeg -f u8 -ac 1 -ar 24000 -i integers.txt -y audio.wav

但是我的 audio.wav 听起来像白噪声.我为 -f -ar 尝试了其他一些值,但是我听到的只是白噪声的频率不同,也许还有一些嗡嗡声.

But my audio.wav just sounds like white noise. I tried a few other values for -f and for -ar, but all I hear are different frequencies of white noise and maybe some extra buzzing.

是否可以使用ffmpeg将整数列表转换为音频文件以供其他计算机播放?如果是这样,什么是正确的ffmpeg命令执行此操作?

Is it possible to use ffmpeg to translate my list of integers into an audio file for other computers to play? If so, what's the correct ffmpeg command to do this?

其他说明

如果有帮助,如果我想听音频,这是我上传到ESP32的草图文件:

If it helps, this is the sketch file that I upload to an ESP32 if I want to hear the audio:

https://github.com/johnlai2004/sound-project/blob/master/play-audio.ino

简而言之,文件如下所示:

In short, the file looks like this:

#define speakerPin 25                          //The pins to output audio on. (9,10 on UNO,Nano)
#define bufferTotal 1347
#define buffSize 32

byte buffer[bufferTotal][buffSize];
int buffItemN = 0;
int bufferN = 0;

hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;

void IRAM_ATTR onTimer() {
  portENTER_CRITICAL_ISR(&timerMux);


  byte v = buffer[bufferN][buffItemN];
  dacWrite(speakerPin,v);

  buffItemN++;

  if(buffItemN >= buffSize){                                      //If the buffer is empty, do the following
    buffItemN = 0;                                              //Reset the sample count
    bufferN++;
    if(bufferN >= bufferTotal)
      bufferN = 0;
  }

  portEXIT_CRITICAL_ISR(&timerMux);

}

void setup() {      

/* buffer records */
buffer[0][0]=88;  // I split the long list of integers and load it into a 2D array
buffer[0][1]=88;
buffer[0][2]=86;
buffer[0][3]=85;
//etc....
buffer[1346][28]=94;
buffer[1346][29]=92;
buffer[1346][30]=92;
buffer[1346][31]=95;


/* end buffer records */

  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 41, true);
  timerAlarmEnable(timer);

}

void loop() {

}

buffer ... 是在 integers.txt 文件中找到的整数列表.

The buffer... is the list of integers found in the integers.txt file.

推荐答案

正如@Gyan在注释中建议的那样,在运行ffmpeg命令之前,我必须先将整数列表转换为二进制文件.因此,我使用以下代码创建了一个名为 main.go 的golang脚本:

As @Gyan suggested in comments, I had to convert my list of integers to a binary file first before running the ffmpeg command. So I created a golang script called main.go with this:

package main

import (
  "io/ioutil"
  "strings"
  "strconv"
  "os"
)
func main() {

  input:="./integers.txt"
  output:="./binary.raw"

  // Load the list of integers into memory
  contentbyte, _ := ioutil.ReadFile(input)
  content := strings.Split(string(contentbyte)," ");

  // Prepare to output a new binary file
  f, err := os.OpenFile(output, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
  if err != nil {
      panic(err)
  }
  defer f.Close()

  for _,val := range content {
    // Convert each integer to a binary value and write to output file
    i,_ := strconv.Atoi(val)
    if _, err = f.Write([]byte{byte(i)}); err != nil {
        panic(err)
    }
  }

}

我运行 go run main.go 给我 binary.raw 文件.然后,我运行了在我的问题中发布的ffmpeg命令,例如 ffmpeg -f u8 -ar 24000 -ac 1 -i binary.raw -y audio.wav .

I run the go run main.go to give me the binary.raw file. I then ran the ffmpeg command as posted in my question like this ffmpeg -f u8 -ar 24000 -ac 1 -i binary.raw -y audio.wav.

audio.wav 文件的声音就像我想要的ESP32 +扬声器的输出一样.

The audio.wav file sounds just like the output of my ESP32 + speaker, which is what I wanted.

这篇关于使用什么ffmpeg命令将无符号整数列表转换为音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆