Answers.Body: <p>I found <a href="https://serverfault.com/a/80897/83850">this method on serverfault.com</a> and it seems to restart ffmpeg on error:</p>
  
  <pre><code>cmd="ffmpeg -re -thread_queue_size 512 -rtsp_transport tcp -i ${source_url} \
     -acodec libmp3lame  -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 \
     -i /dev/zero -vcodec libx264 -preset medium -s 1280x720 -g 6 -r 3    \
     -f flv rtmp://a.rtmp.youtube.com/live2/${youtube_stream_key}"
  
  until $cmd ; do
          echo "restarting ffmpeg command..."
          sleep 2
  done
  </code></pre>
  
  <p>without " " it is showing error: -re: not found </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2016-05-24T14:31:50.057Z
Answers.LastActivityDate: 2020-01-29T21:08:30.933Z
Answers.LastEditDate: 2020-01-29T21:08:30.933Z
Answers.UserDisplayName: steampowered
Body: <p>I use <a href="https://ffmpeg.org/" rel="nofollow">ffmpeg</a> to livestream an Axis camera feed to <a href="https://support.google.com/youtube/answer/2474026?hl=en" rel="nofollow">youtube livestream</a>. I have a beefy server with 32 GB of ram and plenty of CPUs doing the processing, but still the stream errors out from time to time.</p>
  
  <p>Would it be possible to change my bash script so it restarts automatically on error? </p>
  
  <p>This is my ffmpeg command, and I run it from a <code>screen</code> session:</p>
  
  <pre><code>ffmpeg -re -thread_queue_size 512 -rtsp_transport tcp -i "${source_url}" \
     -acodec libmp3lame  -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 \
     -i /dev/zero -vcodec libx264 -preset medium -s 1280x720 -g 6 -r 3    \
     -f flv "rtmp://a.rtmp.youtube.com/live2/${youtube_stream_key}"
  </code></pre>
  
  <p>These are some of the error messages which cause the encoding to error and stop:</p>
  
  <pre><code>Failed to update header with correct duration.
  Failed to update header with correct filesize.
  </code></pre>
  
  <p>this error is rare since I raised the <code>thread_queue_size</code> parameter:</p>
  
  <pre><code>Thread message queue blocking; consider raising the thread_queue_size option
  </code></pre>
  
  <p>I am looking for answers on how to make ffmpeg automatically restart. </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-24T05:14:51.877Z
Comments.Text: Since you don't wish to tweak the FFmpeg syntax and a script will have to watch & restart ffmpeg, this is off-topic here and better placed at Super User or Unix/Ubuntu SE.
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-24T12:24:49.893Z
Comments.Text: @Mulvya your comment is analogous to someone asking how to setup automatic hard drive backup, and you respond "well, you shouldn't need automatic backup - fix all hard drive failure causes instead".  Undoable sir.
Comments.UserDisplayName: steampowered
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-24T12:40:44.270Z
Comments.Text: Your Q is about writing a script to restart a program. It could be any program which exits unexpectedly. That it happens to be ffmpeg is irrelevant, since ffmpeg can't restart itself. You'll have better luck on Super User with the bash tag.
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-25T16:57:34.493Z
Comments.Text: I'm voting to close this question as off-topic because this is specifically about bash scripting rather than Video Production.  The fact that you are running FF-MPEG is immaterial to what you are trying to accomplish (have bash restart a program when it fails) and it would be a duplicate on any sites it could be migrated to (and in fact it appears you already found one such answer).
Comments.UserDisplayName: AJ Henderson
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-25T17:02:52.617Z
Comments.Text: To expand a bit on that close reason.  The key distinction is that there is nothing about being video production related that makes the question unique.  There is not video production or even ff-mpeg specific concern here.  Superuser or ServerFault are both far more qualified to answer the question in the general sense and, in fact, already have.  If it needed to know something about the return of ff-mpeg or you were trying to do it through ff-mpeg itself, it would be on topic, but that doesn't appear to be the case here.
Comments.UserDisplayName: AJ Henderson
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-25T22:05:04.073Z
Comments.Text: Well I didn't know the solution would be a bash solution until one of the commenters suggested looking for a bash solution.  I thought maybe ffmeg had an option or feature to restart on error.  So my question was specific to ffmpeg.  Perhaps I'm the only one who didn't know this would be the answer, or instead perhaps other people video producers would benefit.  You guys seem to have all the answers, so I'll leave it up to you.
Comments.UserDisplayName: steampowered
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-12-22T23:23:18.477Z
Comments.Text: `ffmpeg -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 ....`
Comments.UserDisplayName: Pedro Lobito
Tags: <ffmpeg>
Title: Automatically restart ffmpeg on error from inside bash script
UserDisplayName: steampowered
ThreadText: # Automatically restart ffmpeg on error from inside bash script
  
  (Asked by: username_0 on 2016-05-23)
  
  I use [ffmpeg](https://ffmpeg.org/) to livestream an Axis camera feed to [youtube livestream](https://support.google.com/youtube/answer/2474026?hl=en). I have a beefy server with 32 GB of ram and plenty of CPUs doing the processing, but still the stream errors out from time to time.
  
  Would it be possible to change my bash script so it restarts automatically on error?
  
  This is my ffmpeg command, and I run it from a `screen` session:
  
  ```
  ffmpeg -re -thread_queue_size 512 -rtsp_transport tcp -i "${source_url}" \
     -acodec libmp3lame  -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 \
     -i /dev/zero -vcodec libx264 -preset medium -s 1280x720 -g 6 -r 3    \
     -f flv "rtmp://a.rtmp.youtube.com/live2/${youtube_stream_key}"
  
  ```
  
  These are some of the error messages which cause the encoding to error and stop:
  
  ```
  Failed to update header with correct duration.
  Failed to update header with correct filesize.
  
  ```
  
  this error is rare since I raised the `thread_queue_size` parameter:
  
  ```
  Thread message queue blocking; consider raising the thread_queue_size option
  
  ```
  
  I am looking for answers on how to make ffmpeg automatically restart.
  
  **Tags:** ffmpeg
  
  > **Comments:**
  > **username_1**: Since you don't wish to tweak the FFmpeg syntax and a script will have to watch & restart ffmpeg, this is off-topic here and better placed at Super User or Unix/Ubuntu SE.  
  > **username_0**: @Mulvya your comment is analogous to someone asking how to setup automatic hard drive backup, and you respond "well, you shouldn't need automatic backup - fix all hard drive failure causes instead".  Undoable sir.  
  > **username_1**: Your Q is about writing a script to restart a program. It could be any program which exits unexpectedly. That it happens to be ffmpeg is irrelevant, since ffmpeg can't restart itself. You'll have better luck on Super User with the bash tag.  
  > **username_2**: I'm voting to close this question as off-topic because this is specifically about bash scripting rather than Video Production.  The fact that you are running FF-MPEG is immaterial to what you are trying to accomplish (have bash restart a program when it fails) and it would be a duplicate on any sites it could be migrated to (and in fact it appears you already found one such answer).  
  > **username_2**: To expand a bit on that close reason.  The key distinction is that there is nothing about being video production related that makes the question unique.  There is not video production or even ff-mpeg specific concern here.  Superuser or ServerFault are both far more qualified to answer the question in the general sense and, in fact, already have.  If it needed to know something about the return of ff-mpeg or you were trying to do it through ff-mpeg itself, it would be on topic, but that doesn't appear to be the case here.  
  > **username_0**: Well I didn't know the solution would be a bash solution until one of the commenters suggested looking for a bash solution.  I thought maybe ffmeg had an option or feature to restart on error.  So my question was specific to ffmpeg.  Perhaps I'm the only one who didn't know this would be the answer, or instead perhaps other people video producers would benefit.  You guys seem to have all the answers, so I'll leave it up to you.  
  > **username_3**: `ffmpeg -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 ....`  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-05-24. Score: 3 | Accepted answer
  
  I found [this method on serverfault.com](https://serverfault.com/a/80897/83850) and it seems to restart ffmpeg on error:
  
  ```
  cmd="ffmpeg -re -thread_queue_size 512 -rtsp_transport tcp -i ${source_url} \
     -acodec libmp3lame  -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 \
     -i /dev/zero -vcodec libx264 -preset medium -s 1280x720 -g 6 -r 3    \
     -f flv rtmp://a.rtmp.youtube.com/live2/${youtube_stream_key}"
  
  until $cmd ; do
          echo "restarting ffmpeg command..."
          sleep 2
  done
  
  ```
  
  without " " it is showing error: -re: not found
  
  
  ---
  
  