通过omxplayer从Python进程的内存中播放MP3,而无需写入磁盘 读者背景方法1:使用Shell进行文件描述符处理方法2:使用命名管道方法3:在Python中使用omxplayer

如何解决通过omxplayer从Python进程的内存中播放MP3,而无需写入磁盘 读者背景方法1:使用Shell进行文件描述符处理方法2:使用命名管道方法3:在Python中使用omxplayer

以下代码接收MP3,将其写入磁盘并使用OMXPlayer播放。我想避免在播放MP3之前将其写入磁盘。

song = response.content
file = open("temp.mp3","wb")
file.write(song)
file.close()
response.close()
play_song_subprocess = subprocess.call(['omxplayer','-o','local','--vol','-500','temp.mp3'])

如何消除file.write()? 我正在寻找做这样的事情:

song = response.content
play_song_subprocess = subprocess.call(['omxplayer',song])

但这会导致以下错误: 嵌入的空字节

解决方法

读者背景

建立在聊天和评论中:

  • tiff("test.tiff",units="in",width=5,height=5,res=300) # insert ggplot code dev.off() 导致段错误。
  • cat temp.mp3 | omxplayer -o local --vol -500 /dev/stdin正常工作。

因此,我们可以在...上传递MP3的数据,但不能在stdin上传递(omxplayer -o local --vol -500 /dev/fd/3 3< <(cat temp.mp3)用于控制:暂停,提早退出等)。


方法1:使用Shell进行文件描述符处理

这等效于“方法3”,但是它没有使用非常新的和现代的Python功能来进行FD处理过程,而是启动了omxplayer的副本来完成工作(因此将工作以及更老的Python版本)。

/bin/sh

由于play_from_stdin_sh = ''' exec 3<&0 # copy stdin to FD 3 exec </dev/tty || exec </dev/null # make stdin now be /dev/tty or /dev/null exec omxplayer -o local --vol -500 /dev/fd/3 # play data from FD 3 ''' p = subprocess.Popen(['sh','-c',play_from_stdin_sh],stdin=subprocess.POPEN) p.communicate(song) # passes data in "song" as stdin to the copy of sh 希望使用stdin从其用户那里获取指令,因此我们需要使用其他文件描述符来传递其内容。因此,尽管我们让Python解释器在stdin上传递内容,然后我们将shell复制stdin到FD 3,并在调用{{1}之前用句柄或omxplayer/dev/tty替换了原始stdin }。


方法2:使用命名管道

关于这是否欺骗了“不写磁盘”约束,还有一个问题。它不会将任何MP3数据写入磁盘,但是会创建创建一个文件系统对象,即使写入该对象的数据流了,这两个进程也可以打开该文件系统对象作为彼此连接的方式直接在进程之间进行,而无需写入磁盘。

/dev/null

方法3:在Python中使用omxplayer

我们可以实现文件描述符,方法1使用import tempfile,os,os.path,shutil,subprocess fifo_dir = None try: fifo_dir = tempfile.mkdtemp('mp3-fifodir') fifo_name = os.path.join(fifo_dir,'fifo.mp3') os.mkfifo(fifo_name) # now,we start omxplayer,and tell it to read from the FIFO # as long as it opens it in read mode,it should just hang until something opens # ...the write side of the FIFO,writes content to it,and then closes it. p = subprocess.Popen(['omxplayer','-o','local','--vol','-500',fifo_name]) # this doesn't actually write content to a file on disk! instead,it's written directly # ...to the omxplayer process's handle on the other side of the FIFO. fifo_fd = open(fifo_name,'w') fifo_fd.write(song) fifo_fd.close() p.wait() finally: shutil.rmtree(fifo_dir) 对象的preexec_fn参数在本机Python中使用了shell。考虑:

Popen

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?