简体   繁体   English

在FFMPEG写入文件的同时读取文件

[英]Reading a File while it is being written by FFMPEG

As the title suggests, i am in the process of creating a client/server application where the server(PHP) reads a file which is being written on by ffmpeg and then outputs it to the client(JAVA). 顾名思义,我正在创建一个客户端/服务器应用程序,其中服务器(PHP)读取由ffmpeg写入的文件,然后将其输出到客户端(JAVA)。 I have succeeded in writing the server script in php which initiates the ffmpeg and then after a while starts to read the file and concurrently sends it to the JAVA client, the problem is that after a while the client stops to receive any sort of data and then just quits, For example if i have a 5MB file, it would read only 12KB and then it quits. 我已经成功地用php编写了服务器脚本,该脚本启动了ffmpeg,然后过了一会儿开始读取文件并同时将其发送给JAVA客户端,问题是一段时间后客户端停止接收任何类型的数据,并且然后就退出了,例如,如果我有一个5MB的文件,则它将仅读取12KB,然后退出。 Could someone tell what can be the issue here? 有人可以说出这里的问题吗? Is it on the server side or on the client side. 是在服务器端还是在客户端。 For References i am attaching both the reading of file code in php and the client side code. 作为参考,我同时附上了php中的文件代码读取和客户端代码。

Code For Reading the file while FFMPEG converts the file(PHP): FFMPEG转换文件时用于读取文件的代码(PHP):

$file = 'D:\\'.$destination;
$fp = @fopen($file, 'r');
//ob_end_clean();  
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream'); 
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header('Content-Length: ' . filesize($file));

$buffer = 4096;
while(!feof($fp)) {

usleep(300000);
echo fread($fp, $buffer);
ob_flush();
flush();


}
fclose($fp);
exit();

Code For reading the File transferred by PHP (JAVA): 代码用于读取通过PHP(JAVA)传输的文件:

URL u = new URL(streamURL);

                clientSocket = (HttpURLConnection) u.openConnection();

                if( clientSocket.getResponseCode() == HttpURLConnection.HTTP_OK ){
                //clientSocket.setReadTimeout(0);
                inRemoteStream = clientSocket.getInputStream();


                while (((count = inRemoteStream.read(buf)) != -1)) {
                    offset += count;
                    System.out.println(offset);
                    fileOutputStream.write(buf, 0, count);
                    setVideoOffset(offset, contentLength);
                }

I`ll be very happy if someone can solve this problem for me :) . 如果有人可以为我解决这个问题,我会很高兴的。

Back again. 再次回来。 This can not be technically interpreted as an answer but merely a different logic to accomplish this task i posted so i can safely say that this can be the answer(soft of) :) . 从技术上讲,这不能解释为答案,而只是完成该任务的不同逻辑,因此我可以放心地说,这可以作为答案():)。 What i did was instead of reading the file while it is being written upon, i started the ffmpeg shell exec command with the 'run in background' option and read the file AFTER ffmpeg converted the file, the results were accurate for small files like 4MB but i instantly ran into another problem when i started converting and reading files more than 30MB . 我所做的不是在写入文件时读取文件,而是使用“在后台运行”选项启动了ffmpeg shell exec命令,并在ffmpeg转换文件后读取文件,对于4MB之类的小文件,结果是准确的但是当我开始转换和读取超过30MB的文件时,我立即遇到了另一个问题。 After some in depth research i got to know that when i was converting a 30MB or above file using ffmpeg what happened was after the conversion, the php script was not giving back its control to JAVA applet in order for it to read the stream, so what i did was that i inserted a simple 'http_response_code(200)' after ffmpeg finished converting and saving the file and voila it works. 经过一些深入的研究,我知道当我使用ffmpeg转换30MB或以上的文件时,转换后发生的事情是,php脚本没有将控制权交还给JAVA applet,以便它读取流,因此我所做的是,在ffmpeg完成转换并保存文件后,我插入了一个简单的“ http_response_code(200)”,瞧它就可以了。 For references here is what i did in PHP: 供参考,这是我在PHP中所做的:

http_response_code(200);  
$file = 'D:\\'.$destination;
$fp = fopen($file, 'r');
//ob_end_clean();  
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream'); 
//header("Cache-control: private");
//header('Pragma: private');
//header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header('Content-Length: ' . filesize($file));

$buffer = 1024*8;
while(!feof($fp)) {

echo fread($fp, $buffer);
ob_flush();
flush();



}
fclose($fp);
exit();

Hope it helps anyone who is stuck in a similar problem. 希望它可以帮助陷入类似问题的任何人。

Regards Haris Tasawar. 关于哈里斯·塔萨瓦尔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM